@@ -196,26 +196,42 @@ void NotifyListenerSet(Listener* listener) {
196196 LogInfo (" FCM: Using FCM senderID %s" , senderID.UTF8String );
197197 id appDelegate = [UIApplication sharedApplication ];
198198
199- #if FIREBASE_PLATFORM_IOS
200- // Register for remote notifications. Both codepaths result in
201- // application:didRegisterForRemoteNotificationsWithDeviceToken: being called when they
202- // complete, or application:didFailToRegisterForRemoteNotificationsWithError: if there was an
203- // error. We complete the future there.
204- if (floor (NSFoundationVersionNumber ) <= NSFoundationVersionNumber_iOS_7_1) {
205- // iOS 7.1 or earlier
206- UIRemoteNotificationType allNotificationTypes =
207- (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert |
208- UIRemoteNotificationTypeBadge);
209- [appDelegate registerForRemoteNotificationTypes: allNotificationTypes];
210- } else {
211- // iOS 8 or later
212- UIUserNotificationType allNotificationTypes =
213- (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
214- UIUserNotificationSettings *settings =
215- [UIUserNotificationSettings settingsForTypes: allNotificationTypes categories: nil ];
216- [appDelegate registerUserNotificationSettings: settings];
199+ #if FIREBASE_PLATFORM_IOS || FIREBASE_PLATFORM_TVOS
200+ if ([UNUserNotificationCenter class ] != nil ) {
201+ // iOS 10 or later, and tvOS
202+ // For iOS 10 display notification (sent via APNS)
203+ [UNUserNotificationCenter currentNotificationCenter ].delegate = appDelegate;
204+ UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
205+ UNAuthorizationOptionSound | UNAuthorizationOptionBadge ;
206+ [[UNUserNotificationCenter currentNotificationCenter ]
207+ requestAuthorizationWithOptions: authOptions
208+ completionHandler: ^(BOOL granted, NSError * _Nullable error) {
209+ }];
217210 [appDelegate registerForRemoteNotifications ];
218211 }
212+ #endif // FIREBASE_PLATFORM_IOS || FIREBASE_PLATFORM_TVOS
213+
214+ #if FIREBASE_PLATFORM_IOS
215+ // Register for remote notifications. Both codepaths result in
216+ // application:didRegisterForRemoteNotificationsWithDeviceToken: being called when they
217+ // complete, or application:didFailToRegisterForRemoteNotificationsWithError: if there was an
218+ // error. We complete the future there.
219+ if (floor (NSFoundationVersionNumber ) <= NSFoundationVersionNumber_iOS_7_1) {
220+ // iOS 7.1 or earlier
221+ UIRemoteNotificationType allNotificationTypes =
222+ (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert |
223+ UIRemoteNotificationTypeBadge);
224+ [appDelegate registerForRemoteNotificationTypes: allNotificationTypes];
225+ } else if (floor (NSFoundationVersionNumber ) <= NSFoundationVersionNumber_iOS_9_4) {
226+ // 8.0 <= iOS version <= 9.4
227+ // >= 10.0 is handled by the first if block above.
228+ UIUserNotificationType allNotificationTypes =
229+ (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
230+ UIUserNotificationSettings *settings =
231+ [UIUserNotificationSettings settingsForTypes: allNotificationTypes categories: nil ];
232+ [appDelegate registerUserNotificationSettings: settings];
233+ [appDelegate registerForRemoteNotifications ];
234+ }
219235 #endif // FIREBASE_PLATFORM_IOS
220236
221237 // Only request the token automatically if permitted
@@ -524,11 +540,13 @@ static BOOL AppDelegateApplicationDidFinishLaunchingWithOptions(id self, SEL sel
524540 [user_notification_center setDelegate: (id <UNUserNotificationCenterDelegate >)application];
525541 }
526542
543+ g_message_notification_opened = false ;
527544 #if FIREBASE_PLATFORM_IOS
528545 // If the app was launched with a notification, cache it until we're connected.
529546 g_launch_notification =
530547 [launch_options objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
531548 g_message_notification_opened = g_launch_notification != nil ;
549+ #endif // FIREBASE_PLATFORM_IOS
532550
533551 IMP app_delegate_application_did_finish_launching_with_options =
534552 SwizzledMethodCache ().GetMethodForObject (
@@ -551,7 +569,6 @@ static BOOL AppDelegateApplicationDidFinishLaunchingWithOptions(id self, SEL sel
551569 [invocation getReturnValue: &ret];
552570 return ret;
553571 }
554- #endif // FIREBASE_PLATFORM_IOS
555572 return NO ;
556573}
557574
0 commit comments