When a push notification is received by the device, the application can be in one of the following states:
- Forground: When the app is running and is used by the user right now; in this case, a
notificationReceivedForegroundevent will be fired, do not forget to invokecompletion()callback.
Finally, when a notification is opened by the device user (i.e. tapped-on), a notificationOpened event is fired, here as well you need to remember invoking completion() callback.
Example:
constructor() {
this._boundOnNotificationReceivedForeground = this.onNotificationReceivedForeground.bind(this);
this._boundOnNotificationOpened = this.onNotificationOpened.bind(this);
NotificationsIOS.addEventListener('notificationReceivedForeground', this._boundOnNotificationReceivedForeground);
NotificationsIOS.addEventListener('notificationOpened', this._boundOnNotificationOpened);
}
onNotificationReceivedForeground(notification, completion) {
completion({alert: true, sound: false, badge: false});
console.log("Notification Received - Foreground", notification);
}
onNotificationOpened(notification, completion, action) {
console.log("Notification opened by device user", notification);
console.log(`Notification opened with an action identifier: ${action.identifier} and response text: ${action.text}`, notification);
completion();
}
componentWillUnmount() {
// Don't forget to remove the event listeners to prevent memory leaks!
NotificationsIOS.removeEventListener('notificationReceivedForeground', this._boundOnNotificationReceivedForeground);
NotificationsIOS.removeEventListener('notificationOpened', this._boundOnNotificationOpened);
}When you receive a push notification, you'll get an instance of IOSNotification object, contains the following methods:
getMessage()- returns the notification's main message string.getSound()- returns the sound string from theapsobject.getBadgeCount()- returns the badge count number from theapsobject.getCategory()- returns the category from theapsobject (related to interactive notifications).getData()- returns the data payload (additional info) of the notification.getType()- returnsmanagedfor managed notifications, otherwise returnsregular.
On Android the same core functionality is provided, but using a different API:
import {NotificationsAndroid} from 'react-native-notifications';
// On Android, we allow for only one (global) listener per each event type.
NotificationsAndroid.setNotificationReceivedListener((notification) => {
console.log("Notification received on device in background or foreground", notification.getData());
});
NotificationsAndroid.setNotificationReceivedInForegroundListener((notification) => {
console.log("Notification received on device in foreground", notification.getData());
});
NotificationsAndroid.setNotificationOpenedListener((notification) => {
console.log("Notification opened by device user", notification.getData());
});getData()- content of thedatasection of the original message (sent to GCM).getTitle()- Convenience for returningdata.title.getMessage()- Convenience for returningdata.body.
React-Native's PushNotificationsIOS.getInitialNotification() allows for the async retrieval of the original notification used to open the App on iOS, but it has no equivalent implementation for Android.
While for iOS we nonetheless offer the more elaborate Background Queue solution, on Android we've settled for an implementation similar to React Native's -- An API method PendingNotifications.getInitialNotification(), which returns a promise:
import {NotificationsAndroid, PendingNotifications} from 'react-native-notifications';
PendingNotifications.getInitialNotification()
.then((notification) => {
console.log("Initial notification was:", (notification ? notification.getData() : 'N/A'));
})
.catch((err) => console.error("getInitialNotifiation() failed", err));Note
Notifications are considered 'initial' under the following terms:
- User tapped on a notification, AND -
- App was either not running at all ("dead" state), OR it existed in the background with no running activities associated with it.