@@ -8,14 +8,14 @@ import UserNotifications
88class AppDelegate : NSObject , NSApplicationDelegate {
99 let notifications = NotificationsManager ( )
1010 let navigation : NavigationModel = . shared
11+ let log = Log . scoped ( " AppDelegate " )
1112
1213 func applicationWillFinishLaunching( _ notification: Notification ) {
1314 // Disable native tabbing
1415 NSWindow . allowsAutomaticWindowTabbing = false
1516
1617 // Setup Notifications Delegate
17- notifications. setup ( )
18- UNUserNotificationCenter . current ( ) . delegate = notifications
18+ setupNotifications ( )
1919 }
2020
2121 func applicationDidFinishLaunching( _ aNotification: Notification ) {
@@ -31,12 +31,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
3131 func applicationSupportsSecureRestorableState( _ app: NSApplication ) -> Bool {
3232 return true
3333 }
34+ }
3435
36+ // MARK: - Notifications
37+
38+ extension AppDelegate {
39+ func setupNotifications( ) {
40+ notifications. setup ( )
41+ notifications. onNotificationReceived { response in
42+ self . handleNotification ( response)
43+ }
44+ UNUserNotificationCenter . current ( ) . delegate = notifications
45+ }
46+
3547 func application(
3648 _ application: NSApplication ,
3749 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
3850 ) {
39- Log . shared . debug ( " Registered for remote notifications: \( deviceToken) " )
51+ log . debug ( " Registered for remote notifications: \( deviceToken) " )
4052
4153 notifications. didRegisterForRemoteNotifications ( deviceToken: deviceToken)
4254 }
@@ -45,6 +57,28 @@ class AppDelegate: NSObject, NSApplicationDelegate {
4557 _ application: NSApplication ,
4658 didFailToRegisterForRemoteNotificationsWithError error: Error
4759 ) {
48- Log . shared. error ( " Failed to register for remote notifications \( error) " )
60+ log. error ( " Failed to register for remote notifications \( error) " )
61+ }
62+
63+ func handleNotification( _ response: UNNotificationResponse ) {
64+ log. debug ( " Received notification: \( response) " )
65+
66+ // TODO: Navigate
67+ guard let userInfo = response. notification. request. content. userInfo as? [ String : Any ] else {
68+ return
69+ }
70+
71+ if let peerId = getPeerFromNotification ( userInfo) {
72+ navigation. select ( . chat( peer: peerId) )
73+ // TODO: Handle spaceId
74+ }
75+ }
76+
77+ func getPeerFromNotification( _ userInfo: [ String : Any ] ) -> Peer ? {
78+ if let peerUserId = userInfo [ " userId " ] as? Int64 {
79+ return . user( id: peerUserId)
80+ } else {
81+ return nil
82+ }
4983 }
5084}
0 commit comments