From 68571338b688f4b63f1b771e5a22df209801247e Mon Sep 17 00:00:00 2001 From: Danny van der Jagt Date: Mon, 23 May 2016 12:59:12 +0200 Subject: [PATCH 1/2] [Push notifications] Add error event --- .../PushNotificationIOS.js | 16 +++++++++++---- .../RCTPushNotificationManager.h | 1 + .../RCTPushNotificationManager.m | 20 +++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index 39fbca8c654876..231d89a2047e75 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 = 'remoteNotificationsRegisteredError'; 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..4fc6962b171e78 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 RCTRemoteNotificationRegisteredError = @"RemoteNotificationRegisteredError"; @implementation RCTConvert (UILocalNotification) @@ -85,6 +86,10 @@ - (void)setBridge:(RCTBridge *)bridge selector:@selector(handleRemoteNotificationsRegistered:) name:RCTRemoteNotificationsRegistered object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleRemoteNotificationRegisteredError:) + name:RCTRemoteNotificationRegisteredError + object:nil]; } - (NSDictionary *)constantsToExport @@ -135,6 +140,15 @@ + (void)didReceiveLocalNotification:(UILocalNotification *)notification userInfo:details]; } + + ++ (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error +{ + [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationRegisteredError + 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)handleRemoteNotificationRegisteredError:(NSNotification *)notification +{ + [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegisteredError" + body:[notification userInfo]]; +} + /** * Update the application icon badge number on the home screen */ From 7e75344d5aef1d3b9e92fa8cf69314b506027f8f Mon Sep 17 00:00:00 2001 From: Danny van der Jagt Date: Mon, 23 May 2016 15:18:54 +0200 Subject: [PATCH 2/2] [Push notifications] Added proposed changes --- .../PushNotificationIOS/PushNotificationIOS.js | 4 ++-- .../RCTPushNotificationManager.m | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Libraries/PushNotificationIOS/PushNotificationIOS.js b/Libraries/PushNotificationIOS/PushNotificationIOS.js index 231d89a2047e75..77e120177d7fa6 100644 --- a/Libraries/PushNotificationIOS/PushNotificationIOS.js +++ b/Libraries/PushNotificationIOS/PushNotificationIOS.js @@ -21,7 +21,7 @@ var _initialNotification = RCTPushNotificationManager && var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived'; var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered'; -var NOTIF_REGISTER_ERROR_EVENT = 'remoteNotificationsRegisteredError'; +var NOTIF_REGISTER_ERROR_EVENT = 'remoteNotificationsRegistrationError'; var DEVICE_LOCAL_NOTIF_EVENT = 'localNotificationReceived'; /** @@ -179,7 +179,7 @@ class PushNotificationIOS { handler(registrationInfo.deviceToken); } ); - } else if(type === 'error'){ + } else if (type === 'error'){ listener = RCTDeviceEventEmitter.addListener( NOTIF_REGISTER_ERROR_EVENT, (registrationError) => { diff --git a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m index 4fc6962b171e78..df8f25b1229505 100644 --- a/Libraries/PushNotificationIOS/RCTPushNotificationManager.m +++ b/Libraries/PushNotificationIOS/RCTPushNotificationManager.m @@ -27,7 +27,7 @@ NSString *const RCTLocalNotificationReceived = @"LocalNotificationReceived"; NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived"; NSString *const RCTRemoteNotificationsRegistered = @"RemoteNotificationsRegistered"; -NSString *const RCTRemoteNotificationRegisteredError = @"RemoteNotificationRegisteredError"; +NSString *const RCTRemoteNotificationsRegistrationError = @"RemoteNotificationsRegistrationError"; @implementation RCTConvert (UILocalNotification) @@ -87,8 +87,8 @@ - (void)setBridge:(RCTBridge *)bridge name:RCTRemoteNotificationsRegistered object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(handleRemoteNotificationRegisteredError:) - name:RCTRemoteNotificationRegisteredError + selector:@selector(handleRemoteNotificationsRegistrationError:) + name:RCTRemoteNotificationsRegistrationError object:nil]; } @@ -144,7 +144,7 @@ + (void)didReceiveLocalNotification:(UILocalNotification *)notification + (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { - [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationRegisteredError + [[NSNotificationCenter defaultCenter] postNotificationName:RCTRemoteNotificationsRegistrationError object:self userInfo:error.userInfo]; } @@ -167,10 +167,10 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification body:notification.userInfo]; } -- (void)handleRemoteNotificationRegisteredError:(NSNotification *)notification +- (void)handleRemoteNotificationsRegistrationError:(NSNotification *)notification { - [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegisteredError" - body:[notification userInfo]]; + [_bridge.eventDispatcher sendDeviceEventWithName:@"remoteNotificationsRegistrationError" + body:notification.userInfo]; } /**