From 4616037428ba8c49a465856973fe4a6934a5a045 Mon Sep 17 00:00:00 2001 From: Tadeu Zagallo Date: Fri, 10 Apr 2015 03:10:04 -0700 Subject: [PATCH] [ReactNative] Fix push notifications on iOS7 --- .../RCTPushNotificationManager.m | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m index 17ceb204c..90f2d8786 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m @@ -85,31 +85,51 @@ RCT_EXPORT_METHOD(getApplicationIconBadgeNumber:(RCTResponseSenderBlock)callback RCT_EXPORT_METHOD(requestPermissions) { + Class _UIUserNotificationSettings; + if ((_UIUserNotificationSettings = NSClassFromString(@"UIUserNotificationSettings"))) { + UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert; + UIUserNotificationSettings *notificationSettings = [_UIUserNotificationSettings settingsForTypes:types categories:nil]; + [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; + } else { + #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 - // if we are targeting iOS 7, *and* the new UIUserNotificationSettings - // class is not available, then register using the old mechanism - if (![UIUserNotificationSettings class]) { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert]; - return; - } #endif - UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert; - UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; - [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; + } } RCT_EXPORT_METHOD(checkPermissions:(RCTResponseSenderBlock)callback) { - NSMutableDictionary *permissions = [[NSMutableDictionary alloc] init]; - UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; - permissions[@"alert"] = @((BOOL)(types & UIUserNotificationTypeAlert)); - permissions[@"badge"] = @((BOOL)(types & UIUserNotificationTypeBadge)); - permissions[@"sound"] = @((BOOL)(types & UIUserNotificationTypeSound)); +#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 + +#define UIUserNotificationTypeAlert UIRemoteNotificationTypeAlert +#define UIUserNotificationTypeBadge UIRemoteNotificationTypeBadge +#define UIUserNotificationTypeSound UIRemoteNotificationTypeSound + +#endif + + NSUInteger types; + if ([UIApplication instancesRespondToSelector:@selector(currentUserNotificationSettings)]) { + types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types]; + } else { + +#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 + + types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; + +#endif + + } + + NSMutableDictionary *permissions = [[NSMutableDictionary alloc] init]; + permissions[@"alert"] = @((types & UIUserNotificationTypeAlert) > 0); + permissions[@"badge"] = @((types & UIUserNotificationTypeBadge) > 0); + permissions[@"sound"] = @((types & UIUserNotificationTypeSound) > 0); callback(@[permissions]); }