@@ -8,63 +8,41 @@ import CoreData
88class AppDelegate : UIResponder , UIApplicationDelegate {
99 let tag = " AppDelegate "
1010
11- func application(
12- _ application: UIApplication ,
13- didFinishLaunchingWithOptions launchOptions: [ UIApplication . LaunchOptionsKey : Any ] ?
14- ) -> Bool {
15- Log . d ( tag, " ApplicationDelegate didFinishLaunchingWithOptions. " )
11+ func application( _ application: UIApplication , didFinishLaunchingWithOptions launchOptions: [ UIApplication . LaunchOptionsKey : Any ] ? ) -> Bool {
12+ Log . d ( tag, " Launching AppDelegate " )
1613
17- Messaging . messaging ( ) . delegate = self
18-
19- registerForPushNotifications ( )
14+ // Register app permissions for push notifications
2015 UNUserNotificationCenter . current ( ) . delegate = self
16+ UNUserNotificationCenter . current ( ) . requestAuthorization ( options: [ . alert, . badge, . sound] ) { success, error in
17+ guard success else {
18+ Log . e ( self . tag, " Failed to register for local push notifications " , error)
19+ return
20+ }
21+ Log . d ( self . tag, " Successfully registered for local push notifications " )
22+ }
23+
24+ // Register too receive remote notifications
25+ application. registerForRemoteNotifications ( )
26+
27+ // Set self as messaging delegate
28+ Messaging . messaging ( ) . delegate = self
2129
2230 return true
2331 }
2432
25- func application(
26- _ application: UIApplication ,
27- didFailToRegisterForRemoteNotificationsWithError error: Error
28- ) {
29- Log . e ( tag, " Failed to register for remote notifications " , error)
30- }
31-
32- func application(
33- _ application: UIApplication ,
34- didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
35- ) {
36- let token = deviceToken
37- . map { data in String ( format: " %02.2hhx " , data) }
38- . joined ( )
39- Log . d ( tag, " Registered for remote notifications. Passing APNs token to Firebase: \( token) " )
33+ func application( _ application: UIApplication , didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data ) {
34+ let token = deviceToken. map { data in String ( format: " %02.2hhx " , data) } . joined ( )
4035 Messaging . messaging ( ) . apnsToken = deviceToken
36+ Log . d ( tag, " Registered for remote notifications. Passing APNs token to Firebase: \( token) " )
4137 }
4238
43- func registerForPushNotifications( ) {
44- Log . d ( tag, " Registering for local push notifications " )
45- UNUserNotificationCenter . current ( )
46- . requestAuthorization ( options: [ . alert, . sound, . badge] ) { success, error in
47- guard success else {
48- Log . e ( self . tag, " Failed to register for local push notifications " , error)
49- return
50- }
51- Log . d ( self . tag, " Successfully registered for local push notifications " )
52- self . registerForRemoteNotifications ( )
53- }
54- }
55-
56- func registerForRemoteNotifications( ) {
57- UNUserNotificationCenter . current ( ) . getNotificationSettings { settings in
58- print ( " Notification settings: \( settings) " )
59- guard settings. authorizationStatus == . authorized else { return }
60- DispatchQueue . main. async {
61- UIApplication . shared. registerForRemoteNotifications ( )
62- }
63- }
39+ func application( _ application: UIApplication , didFailToRegisterForRemoteNotificationsWithError error: Error ) {
40+ Log . e ( tag, " Failed to register for remote notifications " , error)
6441 }
6542}
6643
6744extension AppDelegate : UNUserNotificationCenterDelegate {
45+ /// Executed when the app is in the foreground. Nothing has to be done here, except call the completionHandler.
6846 func userNotificationCenter(
6947 _ center: UNUserNotificationCenter ,
7048 willPresent notification: UNNotification ,
@@ -75,13 +53,15 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
7553 completionHandler ( [ [ . banner, . sound] ] )
7654 }
7755
56+ /// Executed when the user clicks on the notification.
7857 func userNotificationCenter(
7958 _ center: UNUserNotificationCenter ,
8059 didReceive response: UNNotificationResponse ,
8160 withCompletionHandler completionHandler: @escaping ( ) -> Void
8261 ) {
8362 let userInfo = response. notification. request. content. userInfo
8463 Log . d ( tag, " Notification received via userNotificationCenter(didReceive) " , userInfo)
64+ // TODO: This should navigate to the detail view
8565 completionHandler ( )
8666 }
8767}
0 commit comments