From 4f89fa9cf32bb77d60926dd826ab828ab540986f Mon Sep 17 00:00:00 2001 From: Ian MacLeod Date: Tue, 6 Sep 2016 11:01:31 -0700 Subject: [PATCH] registration error event Summary: This is an updated version of #2336 and #7694. --- This adds a `registrationError` event that is emitted by `PushNotificationIOS` whenever an application receives a registration error from APNS (APNS service failure, running on simulator, etc). This event fires to the exclusion of the `register` event (and vice versa). **How to use** Add the following to your `AppDelegate.m`: ```obj-c - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { [RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error]; } ``` And register an event handler for the event: ```js PushNotificationIOS.addEventListener('registrationError', function({ message, code }) { // Complete your registration process in error. }); ``` **Test plan** Added support for this event (and `register`) to UIExplorer as a proof of concept. Navigating to the push notifications example on a simulator is an easy way to reproduce this e Closes https://github.com/facebook/react-native/pull/9650 Differential Revision: D3822142 Pulled By: javache fbshipit-source-id: a15ed8941b74dc3eed2c44c658deccbcaf39ce3d --- Examples/UIExplorer/UIExplorer/AppDelegate.m | 33 ++++++++++ .../js/PushNotificationIOSExample.js | 40 ++++++++++--- .../PushNotificationIOS.js | 60 +++++++++++++++---- .../RCTPushNotificationManager.h | 1 + .../RCTPushNotificationManager.m | 26 +++++++- 5 files changed, 140 insertions(+), 20 deletions(-) diff --git a/Examples/UIExplorer/UIExplorer/AppDelegate.m b/Examples/UIExplorer/UIExplorer/AppDelegate.m index 822ea8aef..7ca8e039d 100644 --- a/Examples/UIExplorer/UIExplorer/AppDelegate.m +++ b/Examples/UIExplorer/UIExplorer/AppDelegate.m @@ -19,6 +19,7 @@ #import "RCTJavaScriptLoader.h" #import "RCTLinkingManager.h" #import "RCTRootView.h" +#import "RCTPushNotificationManager.h" @interface AppDelegate() @@ -76,4 +77,36 @@ onComplete:loadCallback]; } +# pragma mark - Push Notifications + +// Required to register for notifications +- (void)application:(__unused UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings +{ + [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings]; +} + +// Required for the remoteNotificationsRegistered event. +- (void)application:(__unused UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken +{ + [RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; +} + +// Required for the remoteNotificationRegistrationError event. +- (void)application:(__unused UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error +{ + [RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error]; +} + +// Required for the remoteNotificationReceived event. +- (void)application:(__unused UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification +{ + [RCTPushNotificationManager didReceiveRemoteNotification:notification]; +} + +// Required for the localNotificationReceived event. +- (void)application:(__unused UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification +{ + [RCTPushNotificationManager didReceiveLocalNotification:notification]; +} + @end diff --git a/Examples/UIExplorer/js/PushNotificationIOSExample.js b/Examples/UIExplorer/js/PushNotificationIOSExample.js index e0c961b49..b9bafe2a2 100644 --- a/Examples/UIExplorer/js/PushNotificationIOSExample.js +++ b/Examples/UIExplorer/js/PushNotificationIOSExample.js @@ -50,16 +50,18 @@ class Button extends React.Component { class NotificationExample extends React.Component { componentWillMount() { - // Add listener for push notifications - PushNotificationIOS.addEventListener('notification', this._onNotification); - // Add listener for local notifications + PushNotificationIOS.addEventListener('register', this._onRegistered); + PushNotificationIOS.addEventListener('registrationError', this._onRegistrationError); + PushNotificationIOS.addEventListener('notification', this._onRemoteNotification); PushNotificationIOS.addEventListener('localNotification', this._onLocalNotification); + + PushNotificationIOS.requestPermissions(); } componentWillUnmount() { - // Remove listener for push notifications - PushNotificationIOS.removeEventListener('notification', this._onNotification); - // Remove listener for local notifications + PushNotificationIOS.removeEventListener('register', this._onRegistered); + PushNotificationIOS.removeEventListener('registrationError', this._onRegistrationError); + PushNotificationIOS.removeEventListener('notification', this._onRemoteNotification); PushNotificationIOS.removeEventListener('localNotification', this._onLocalNotification); } @@ -101,7 +103,29 @@ class NotificationExample extends React.Component { }); } - _onNotification(notification) { + _onRegistered(deviceToken) { + AlertIOS.alert( + 'Registered For Remote Push', + `Device Token: ${deviceToken}`, + [{ + text: 'Dismiss', + onPress: null, + }] + ); + } + + _onRegistrationError(error) { + AlertIOS.alert( + 'Failed To Register For Remote Push', + `Error (${error.code}): ${error.message}`, + [{ + text: 'Dismiss', + onPress: null, + }] + ); + } + + _onRemoteNotification(notification) { AlertIOS.alert( 'Push Notification Received', 'Alert message: ' + notification.getMessage(), @@ -170,8 +194,6 @@ exports.examples = [ { title: 'Badge Number', render(): ReactElement { - PushNotificationIOS.requestPermissions(); - return (