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
This commit is contained in:
Ian MacLeod
2016-09-06 11:01:31 -07:00
committed by Facebook Github Bot
parent dea6b0e24c
commit 4f89fa9cf3
5 changed files with 140 additions and 20 deletions

View File

@@ -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<any> {
PushNotificationIOS.requestPermissions();
return (
<View>
<Button