Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class NotificationInstance {

func dismissWithUserAction() {
RustBridge.onDismiss(key: key)
SystemNotificationCenter.shared.removeFromNotificationCenter(key: key)
dismiss()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import Foundation
import UserNotifications

class SystemNotificationCenter: NSObject, UNUserNotificationCenterDelegate {
static let shared = SystemNotificationCenter()

private var isAuthorized = false

private override init() {
super.init()
UNUserNotificationCenter.current().delegate = self
}

func requestAuthorization() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
[weak self] granted, error in
if let error = error {
print("Notification authorization error: \(error)")
return
}
self?.isAuthorized = granted
}
}

func postToNotificationCenter(payload: NotificationPayload) {
guard isAuthorized else {
requestAuthorization()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
self?.postNotificationInternal(payload: payload)
}
return
}

postNotificationInternal(payload: payload)
}

private func postNotificationInternal(payload: NotificationPayload) {
let content = UNMutableNotificationContent()
content.title = payload.title
content.body = payload.message
content.sound = .default
content.userInfo = ["key": payload.key]

if let eventDetails = payload.eventDetails {
var subtitle = eventDetails.what
if let location = eventDetails.location, !location.isEmpty {
subtitle += " • \(location)"
}
content.subtitle = subtitle
}

let request = UNNotificationRequest(
identifier: payload.key,
content: content,
trigger: nil
)

UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("Failed to add notification to center: \(error)")
}
}
}

func removeFromNotificationCenter(key: String) {
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [key])
}

func removeAllFromNotificationCenter() {
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
}

func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
completionHandler([])
}

func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
let key = response.notification.request.identifier

switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
RustBridge.onCollapsedConfirm(key: key)
case UNNotificationDismissActionIdentifier:
RustBridge.onDismiss(key: key)
default:
break
}

completionHandler()
}
}
2 changes: 2 additions & 0 deletions crates/notification-macos/swift-lib/src/lib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public func _showNotification(jsonPayload: SRString) -> Bool {
}

NotificationManager.shared.show(payload: payload)
SystemNotificationCenter.shared.postToNotificationCenter(payload: payload)

Thread.sleep(forTimeInterval: 0.1)
return true
Expand All @@ -20,5 +21,6 @@ public func _showNotification(jsonPayload: SRString) -> Bool {
@_cdecl("_dismiss_all_notifications")
public func _dismissAllNotifications() -> Bool {
NotificationManager.shared.dismissAll()
SystemNotificationCenter.shared.removeAllFromNotificationCenter()
return true
}
Loading