Schedules the localNotification for immediate presentation.
details is an object containing:
alertBody : The message displayed in the notification alert.alertAction : The "action" displayed beneath an actionable notification. Defaults to "view";soundName : The sound played when the notification is fired (optional).category : The category of this notification, required for actionable notifications (optional).userInfo : An optional object containing additional notification data.applicationIconBadgeNumber (optional) : The number to display as the app's icon badge. The default value of this property is 0, which means that no badge is displayed.Schedules the localNotification for future presentation.
details is an object containing:
fireDate : The date and time when the system should deliver the notification.alertBody : The message displayed in the notification alert.alertAction : The "action" displayed beneath an actionable notification. Defaults to "view";soundName : The sound played when the notification is fired (optional).category : The category of this notification, required for actionable notifications (optional).userInfo : An optional object containing additional notification data.applicationIconBadgeNumber (optional) : The number to display as the app's icon badge. Setting the number to 0 removes the icon badge.Cancels all scheduled localNotifications
Sets the badge number for the app icon on the home screen
Gets the current badge number for the app icon on the home screen
Cancel local notifications.
Optionally restricts the set of canceled notifications to those
notifications whose userInfo fields match the corresponding fields
in the userInfo argument.
Gets the local notifications that are currently scheduled.
Attaches a listener to remote or local notification events while the app is running in the foreground or the background.
Valid events are:
notification : Fired when a remote notification is received. The
handler will be invoked with an instance of PushNotificationIOS.localNotification : Fired when a local notification is received. The
handler will be invoked with an instance of PushNotificationIOS.register: Fired when the user registers for remote notifications. The
-handler will be invoked with a hex string representing the deviceToken.Removes the event listener. Do this in componentWillUnmount to prevent
+handler will be invoked with a hex string representing the deviceToken.
registrationError: Fired when the user fails to register for remote
+notifications. Typically occurs when APNS is having issues, or the device
+is a simulator. The handler will be invoked with
+{message: string, code: number, details: any}.Removes the event listener. Do this in componentWillUnmount to prevent
memory leaks
Requests notification permissions from iOS, prompting the user's dialog box. By default, it will request all notification permissions, but a subset of these can be requested by passing a map of requested @@ -75,17 +79,19 @@ class Button extends NotificationExample extends React.Component { componentWillMount() { - // Add listener for push notifications - PushNotificationIOS.addEventListener('notification', this._onNotification); - // Add listener for local notifications - PushNotificationIOS.addEventListener('localNotification', this._onLocalNotification); + 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('localNotification', this._onLocalNotification); + PushNotificationIOS.removeEventListener('register', this._onRegistered); + PushNotificationIOS.removeEventListener('registrationError', this._onRegistrationError); + PushNotificationIOS.removeEventListener('notification', this._onRemoteNotification); + PushNotificationIOS.removeEventListener('localNotification', this._onLocalNotification); } render() { @@ -126,7 +132,29 @@ class NotificationExample extends }); } - _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(), @@ -195,8 +223,6 @@ exports.examples { title: 'Badge Number', render(): ReactElement<any> { - PushNotificationIOS.requestPermissions(); - return ( <View> <Button