From efc055075ca70b93f690f33f9eac6125d66f5b79 Mon Sep 17 00:00:00 2001 From: Libin Lu Date: Wed, 21 Sep 2016 20:38:02 -0400 Subject: [PATCH] iOS 10 support --- ios/RNFIRMesssaging.m | 59 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/ios/RNFIRMesssaging.m b/ios/RNFIRMesssaging.m index 0bac0d4..ef24f7e 100644 --- a/ios/RNFIRMesssaging.m +++ b/ios/RNFIRMesssaging.m @@ -5,6 +5,8 @@ #import "RCTEventDispatcher.h" #import "RCTUtils.h" +@import UserNotifications; + #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 #define UIUserNotificationTypeAlert UIRemoteNotificationTypeAlert @@ -187,20 +189,57 @@ RCT_EXPORT_METHOD(requestPermissions) if (RCTRunningInAppExtension()) { return; } - - UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound; - - UIApplication *app = RCTSharedApplication(); - if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) { - UIUserNotificationSettings *notificationSettings = - [UIUserNotificationSettings settingsForTypes:(NSUInteger)types categories:nil]; - [app registerUserNotificationSettings:notificationSettings]; - [app registerForRemoteNotifications]; + if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) { + UIUserNotificationType allNotificationTypes = + (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); + UIApplication *app = RCTSharedApplication(); + if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) { + //iOS 8 or later + UIUserNotificationSettings *notificationSettings = + [UIUserNotificationSettings settingsForTypes:(NSUInteger)allNotificationTypes categories:nil]; + [app registerUserNotificationSettings:notificationSettings]; + } else { + //iOS 7 or below + [app registerForRemoteNotificationTypes:(NSUInteger)allNotificationTypes]; + } } else { - [app registerForRemoteNotificationTypes:(NSUInteger)types]; + // iOS 10 or later +#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 + UNAuthorizationOptions authOptions = + UNAuthorizationOptionAlert + | UNAuthorizationOptionSound + | UNAuthorizationOptionBadge; + [[UNUserNotificationCenter currentNotificationCenter] + requestAuthorizationWithOptions:authOptions + completionHandler:^(BOOL granted, NSError * _Nullable error) { + } + ]; + + // For iOS 10 display notification (sent via APNS) + [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; + // For iOS 10 data message (sent via FCM) + [[FIRMessaging messaging] setRemoteMessageDelegate:self]; +#endif } + + [[UIApplication sharedApplication] registerForRemoteNotifications]; +} + +#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 +// Receive displayed notifications for iOS 10 devices. +- (void)userNotificationCenter:(UNUserNotificationCenter *)center + willPresentNotification:(UNNotification *)notification + withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { + NSDictionary *userInfo = notification.request.content.userInfo; + [_bridge.eventDispatcher sendDeviceEventWithName:FCMNotificationReceived body:userInfo]; } +// Receive data message on iOS 10 devices. +- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage { + [_bridge.eventDispatcher sendDeviceEventWithName:FCMNotificationReceived body:[remoteMessage appData]]; +} +#endif + RCT_EXPORT_METHOD(subscribeToTopic: (NSString*) topic) { [[FIRMessaging messaging] subscribeToTopic:topic]; -- 2.26.2