diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index c71722ae89761e..40f753e13b10c5 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -22,6 +22,7 @@ var _initialNotification = RCTPushNotificationManager && var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived'; var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered'; +var NOTIF_REGISTER_ERROR_EVENT = 'remoteNotificationsRegisteredError'; /** * Handle push notifications for your app, including permission handling and @@ -88,8 +89,8 @@ class PushNotificationIOS { */ static addEventListener(type: string, handler: Function) { invariant( - type === 'notification' || type === 'register', - 'PushNotificationIOS only supports `notification` and `register` events' + type === 'notification' || type === 'register' || type === 'error', + 'PushNotificationIOS only supports `notification`, `register` and `error` events' ); var listener; if (type === 'notification') { @@ -106,6 +107,17 @@ class PushNotificationIOS { handler(registrationInfo.deviceToken); } ); + } else if(type === 'error'){ + listener = RCTDeviceEventEmitter.addListener( + NOTIF_REGISTER_ERROR_EVENT, + (registrationError) => { + var key = Object.keys(registrationError)[0] || null; + if(!key){ + return; + } + handler(registrationError[key], key); + } + ); } _notifHandlers.set(handler, listener); } @@ -180,8 +192,8 @@ class PushNotificationIOS { */ static removeEventListener(type: string, handler: Function) { invariant( - type === 'notification' || type === 'register', - 'PushNotificationIOS only supports `notification` and `register` events' + type === 'notification' || type === 'register' || type === 'error', + 'PushNotificationIOS only supports `notification`, `register` and `error` events' ); var listener = _notifHandlers.get(handler); if (!listener) { diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.h b/Libraries/PushNotificationIOS/RCTPushNotificationManager.h index 194bbc5ddeeda4..3b372679b25de6 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.h +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.h @@ -16,5 +16,6 @@ + (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings; + (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken; + (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification; ++ (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; @end diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m index fda9980f9102ff..d8eb7f63341cc4 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m @@ -26,6 +26,7 @@ NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived"; NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered"; +NSString *const RCTRemoteNotificationRegisteredError = @"RemoteNotificationRegisteredError"; @implementation RCTConvert (UILocalNotification) @@ -60,6 +61,10 @@ - (instancetype)init selector:@selector(handleRemoteNotificationsRegistered:) name:RCTRemoteNotificationsRegistered object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleRemoteNotificationRegisteredError:) + name:RCTRemoteNotificationRegisteredError + object:nil]; } return self; } @@ -105,6 +110,13 @@ + (void)application:(__unused UIApplication *)application didReceiveRemoteNotifi userInfo:notification]; } ++ (void)application:(__unused UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error +{ + [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationRegisteredError + object:self + userInfo:error.userInfo]; +} + - (void)handleRemoteNotificationReceived:(NSNotification *)notification { [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationReceived" @@ -116,6 +128,11 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegistered" body:notification.userInfo]; } +- (void)handleRemoteNotificationRegisteredError:(NSNotification *)notification +{ + [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegisteredError" + body:[notification userInfo]]; +} /** * Update the application icon badge number on the home screen @@ -152,17 +169,17 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound; } -#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0 +if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { id notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; -#else +}else{ [[UIApplication sharedApplication] registerForRemoteNotificationTypes:types]; -#endif +} }