@@ -8,14 +8,14 @@ import UserNotifications
8
8
class AppDelegate : NSObject , NSApplicationDelegate {
9
9
let notifications = NotificationsManager ( )
10
10
let navigation : NavigationModel = . shared
11
+ let log = Log . scoped ( " AppDelegate " )
11
12
12
13
func applicationWillFinishLaunching( _ notification: Notification ) {
13
14
// Disable native tabbing
14
15
NSWindow . allowsAutomaticWindowTabbing = false
15
16
16
17
// Setup Notifications Delegate
17
- notifications. setup ( )
18
- UNUserNotificationCenter . current ( ) . delegate = notifications
18
+ setupNotifications ( )
19
19
}
20
20
21
21
func applicationDidFinishLaunching( _ aNotification: Notification ) {
@@ -31,12 +31,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
31
31
func applicationSupportsSecureRestorableState( _ app: NSApplication ) -> Bool {
32
32
return true
33
33
}
34
+ }
34
35
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
+
35
47
func application(
36
48
_ application: NSApplication ,
37
49
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
38
50
) {
39
- Log . shared . debug ( " Registered for remote notifications: \( deviceToken) " )
51
+ log . debug ( " Registered for remote notifications: \( deviceToken) " )
40
52
41
53
notifications. didRegisterForRemoteNotifications ( deviceToken: deviceToken)
42
54
}
@@ -45,6 +57,28 @@ class AppDelegate: NSObject, NSApplicationDelegate {
45
57
_ application: NSApplication ,
46
58
didFailToRegisterForRemoteNotificationsWithError error: Error
47
59
) {
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
+ }
49
83
}
50
84
}
0 commit comments