11import FirebaseCore
2+ import FirebaseMessaging
23import GoogleSignIn
34import UIKit
5+ import UserNotifications
46
57@main
68class AppDelegate : UIResponder , UIApplicationDelegate {
9+ let gcmMessageIDKey = " gcm.message_id "
10+
711 func application( _: UIApplication , open url: URL , options _: [ UIApplication . OpenURLOptionsKey : Any ] = [ : ] ) -> Bool {
812 return GIDSignIn . sharedInstance. handle ( url)
913 }
1014
11- func application( _: UIApplication , didFinishLaunchingWithOptions _: [ UIApplication . LaunchOptionsKey : Any ] ? ) -> Bool {
15+ func application( _ application : UIApplication , didFinishLaunchingWithOptions _: [ UIApplication . LaunchOptionsKey : Any ] ? ) -> Bool {
1216 // Override point for customization after application launch.
1317
1418 FirebaseApp . configure ( )
1519
20+ Messaging . messaging ( ) . delegate = self
21+
22+ if #available( iOS 10 . 0 , * ) {
23+ // For iOS 10 display notification (sent via APNS)
24+ UNUserNotificationCenter . current ( ) . delegate = self
25+
26+ let authOptions : UNAuthorizationOptions = [ . alert, . badge, . sound]
27+ UNUserNotificationCenter . current ( ) . requestAuthorization (
28+ options: authOptions,
29+ completionHandler: { _, _ in }
30+ )
31+ } else {
32+ let settings : UIUserNotificationSettings =
33+ . init( types: [ . alert, . badge, . sound] , categories: nil )
34+ application. registerUserNotificationSettings ( settings)
35+ }
36+
37+ application. registerForRemoteNotifications ( )
38+
1639 return true
1740 }
1841
@@ -29,4 +52,78 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2952 // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
3053 // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
3154 }
55+
56+ func application( _: UIApplication ,
57+ didReceiveRemoteNotification userInfo: [ AnyHashable : Any ] )
58+ {
59+ if let messageID = userInfo [ gcmMessageIDKey] {
60+ print ( " Message ID: \( messageID) " )
61+ }
62+ print ( userInfo)
63+ }
64+
65+ func application( _: UIApplication ,
66+ didReceiveRemoteNotification userInfo: [ AnyHashable : Any ] ) async
67+ -> UIBackgroundFetchResult
68+ {
69+ if let messageID = userInfo [ gcmMessageIDKey] {
70+ print ( " Message ID: \( messageID) " )
71+ }
72+ print ( userInfo)
73+
74+ return UIBackgroundFetchResult . newData
75+ }
76+
77+ func application( _: UIApplication ,
78+ didFailToRegisterForRemoteNotificationsWithError error: Error )
79+ {
80+ print ( " Unable to register for remote notifications: \( error. localizedDescription) " )
81+ }
82+
83+ func application( _: UIApplication ,
84+ didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data )
85+ {
86+ print ( " APNs token retrieved: \( deviceToken) " )
87+ }
88+ }
89+
90+ extension AppDelegate : UNUserNotificationCenterDelegate {
91+ func userNotificationCenter( _: UNUserNotificationCenter ,
92+ willPresent notification: UNNotification ) async
93+ -> UNNotificationPresentationOptions
94+ {
95+ let userInfo = notification. request. content. userInfo
96+
97+ if let messageID = userInfo [ gcmMessageIDKey] {
98+ print ( " Message ID: \( messageID) " )
99+ }
100+
101+ print ( userInfo)
102+ return [ [ . alert, . sound] ]
103+ }
104+
105+ func userNotificationCenter( _: UNUserNotificationCenter ,
106+ didReceive response: UNNotificationResponse ) async
107+ {
108+ let userInfo = response. notification. request. content. userInfo
109+
110+ if let messageID = userInfo [ gcmMessageIDKey] {
111+ print ( " Message ID: \( messageID) " )
112+ }
113+ print ( userInfo)
114+ }
115+ }
116+
117+ extension AppDelegate : MessagingDelegate {
118+ func messaging( _: Messaging , didReceiveRegistrationToken fcmToken: String ? ) {
119+ print ( " Firebase registration token: \( String ( describing: fcmToken) ) " )
120+
121+ let dataDict : [ String : String ] = [ " token " : fcmToken ?? " " ]
122+ NotificationCenter . default. post (
123+ name: Notification . Name ( " FCMToken " ) ,
124+ object: nil ,
125+ userInfo: dataDict
126+ )
127+ // TODO: If necessary send token to application server.
128+ }
32129}
0 commit comments