From 22a382b9d7fdfcde3cc32ac226dd61f013196408 Mon Sep 17 00:00:00 2001 From: Danny van der Jagt Date: Fri, 14 Aug 2015 11:52:05 +0200 Subject: [PATCH 1/3] [Push notifications] Add error event. --- .../PushNotificationIOS.js | 17 ++++++++++---- .../RCTPushNotificationManager.h | 1 + .../RCTPushNotificationManager.m | 23 ++++++++++++++++--- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index c71722ae89761e..6902434d49657e 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,14 @@ class PushNotificationIOS { handler(registrationInfo.deviceToken); } ); + } else if(type === 'error'){ + listener = RCTDeviceEventEmitter.addListener( + NOTIF_REGISTER_ERROR_EVENT, + (registrationError) => { + var key = Object.keys(registrationError)[0]; + handler(key, registrationError[key]); + } + ); } _notifHandlers.set(handler, listener); } @@ -180,8 +189,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 +} } From 75c54f24fc1f6ee58e542041c312f81263528e6d Mon Sep 17 00:00:00 2001 From: Danny van der Jagt Date: Fri, 14 Aug 2015 12:15:29 +0200 Subject: [PATCH 2/3] [Push notifications] Switched error key with error message. --- Libraries/PushNotificationIOS/PushNotificationIOS.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index 6902434d49657e..0c3e6d0913b851 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -112,7 +112,7 @@ class PushNotificationIOS { NOTIF_REGISTER_ERROR_EVENT, (registrationError) => { var key = Object.keys(registrationError)[0]; - handler(key, registrationError[key]); + handler(registrationError[key], key); } ); } From a1c30190f76e058155bee9c0c08484f97a7e3b15 Mon Sep 17 00:00:00 2001 From: Danny van der Jagt Date: Sun, 6 Sep 2015 14:22:47 +0200 Subject: [PATCH 3/3] [Push notifications] Made the key abstracting safer. --- Libraries/PushNotificationIOS/PushNotificationIOS.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index 0c3e6d0913b851..40f753e13b10c5 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -111,7 +111,10 @@ class PushNotificationIOS { listener = RCTDeviceEventEmitter.addListener( NOTIF_REGISTER_ERROR_EVENT, (registrationError) => { - var key = Object.keys(registrationError)[0]; + var key = Object.keys(registrationError)[0] || null; + if(!key){ + return; + } handler(registrationError[key], key); } );