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 (