From d16e0a257983b1156b348c2de2b4c9d2ddd6aa13 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Thu, 9 Apr 2015 09:08:34 -0700 Subject: [PATCH] [ReactNative] Fix push notifications on iOS7 | Tadeu Zagallo --- .../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]); }