Commit efc05507 authored by Libin Lu's avatar Libin Lu

iOS 10 support

parent 486cc0bb
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#import "RCTEventDispatcher.h" #import "RCTEventDispatcher.h"
#import "RCTUtils.h" #import "RCTUtils.h"
@import UserNotifications;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
#define UIUserNotificationTypeAlert UIRemoteNotificationTypeAlert #define UIUserNotificationTypeAlert UIRemoteNotificationTypeAlert
...@@ -187,20 +189,57 @@ RCT_EXPORT_METHOD(requestPermissions) ...@@ -187,20 +189,57 @@ RCT_EXPORT_METHOD(requestPermissions)
if (RCTRunningInAppExtension()) { if (RCTRunningInAppExtension()) {
return; return;
} }
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound; UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIApplication *app = RCTSharedApplication(); UIApplication *app = RCTSharedApplication();
if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) { if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) {
//iOS 8 or later
UIUserNotificationSettings *notificationSettings = UIUserNotificationSettings *notificationSettings =
[UIUserNotificationSettings settingsForTypes:(NSUInteger)types categories:nil]; [UIUserNotificationSettings settingsForTypes:(NSUInteger)allNotificationTypes categories:nil];
[app registerUserNotificationSettings:notificationSettings]; [app registerUserNotificationSettings:notificationSettings];
[app registerForRemoteNotifications];
} else { } else {
[app registerForRemoteNotificationTypes:(NSUInteger)types]; //iOS 7 or below
[app registerForRemoteNotificationTypes:(NSUInteger)allNotificationTypes];
}
} else {
// 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) RCT_EXPORT_METHOD(subscribeToTopic: (NSString*) topic)
{ {
[[FIRMessaging messaging] subscribeToTopic:topic]; [[FIRMessaging messaging] subscribeToTopic:topic];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment