diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index 39fbca8c654876..77e120177d7fa6 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -21,6 +21,7 @@ var _initialNotification = RCTPushNotificationManager && var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived'; var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered'; +var NOTIF_REGISTER_ERROR_EVENT = 'remoteNotificationsRegistrationError'; var DEVICE_LOCAL_NOTIF_EVENT = 'localNotificationReceived'; /** @@ -153,8 +154,8 @@ class PushNotificationIOS { */ static addEventListener(type: string, handler: Function) { invariant( - type === 'notification' || type === 'register' || type === 'localNotification', - 'PushNotificationIOS only supports `notification`, `register` and `localNotification` events' + type === 'notification' || type === 'register' || type === 'localNotification' || type === 'error', + 'PushNotificationIOS only supports `notification`, `register`, `error` and `localNotification` events' ); var listener; if (type === 'notification') { @@ -178,6 +179,13 @@ class PushNotificationIOS { handler(registrationInfo.deviceToken); } ); + } else if (type === 'error'){ + listener = RCTDeviceEventEmitter.addListener( + NOTIF_REGISTER_ERROR_EVENT, + (registrationError) => { + handler(registrationError); + } + ); } _notifHandlers.set(handler, listener); } @@ -252,8 +260,8 @@ class PushNotificationIOS { */ static removeEventListener(type: string, handler: Function) { invariant( - type === 'notification' || type === 'register' || type === 'localNotification', - 'PushNotificationIOS only supports `notification`, `register` and `localNotification` events' + type === 'notification' || type === 'register' || type === 'localNotification' || type === 'error', + 'PushNotificationIOS only supports `notification`, `register`, `error` and `localNotification` events' ); var listener = _notifHandlers.get(handler); if (!listener) { diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.h b/Libraries/PushNotificationIOS/RCTPushNotificationManager.h index b7ab540151b5ce..2bca6668b852e4 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.h +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.h @@ -17,5 +17,6 @@ + (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken; + (void)didReceiveRemoteNotification:(NSDictionary *)notification; + (void)didReceiveLocalNotification:(UILocalNotification *)notification; ++ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; @end diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m index 0d6453a36095f6..df8f25b1229505 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m @@ -27,6 +27,7 @@ NSString *const RCTLocalNotificationReceived = @"LocalNotificationReceived"; NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived"; NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered"; +NSString *const RCTRemoteNotificationsRegistrationError = @"RemoteNotificationsRegistrationError"; @implementation RCTConvert (UILocalNotification) @@ -85,6 +86,10 @@ - (void)setBridge:(RCTBridge *)bridge selector:@selector(handleRemoteNotificationsRegistered:) name:RCTRemoteNotificationsRegistered object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleRemoteNotificationsRegistrationError:) + name:RCTRemoteNotificationsRegistrationError + object:nil]; } - (NSDictionary *)constantsToExport @@ -135,6 +140,15 @@ + (void)didReceiveLocalNotification:(UILocalNotification *)notification userInfo:details]; } + + ++ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error +{ + [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationsRegistrationError + object:self + userInfo:error.userInfo]; +} + - (void)handleLocalNotificationReceived:(NSNotification *)notification { [_bridge.eventDispatcher sendDeviceEventWithName:@"localNotificationReceived" @@ -153,6 +167,12 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification body:notification.userInfo]; } +- (void)handleRemoteNotificationsRegistrationError:(NSNotification *)notification +{ + [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegistrationError" + body:notification.userInfo]; +} + /** * Update the application icon badge number on the home screen */