React Native: Integrating Push Notifications using FCM
async createNotificationListeners() {
/*
* Triggered when a particular notification has been received in foreground
* */
this.notificationListener = firebase
.notifications()
.onNotification(notification => {
console.log("fg", notification);
const { title, body } = notification;
this.showAlert(title, body);
});
/*
* If app is in background, listen for when a notification is clicked / tapped / opened as follows:
* */
this.notificationOpenedListener = firebase
.notifications()
.onNotificationOpened(notificationOpen => {
console.log("bg", notificationOpen);
const { title, body } = notificationOpen.notification;
this.showAlert(title, body);
});
/*
* If app is closed, check if it was opened by a notification being clicked / tapped / opened as follows:
* */
const notificationOpen = await firebase
.notifications()
.getInitialNotification();
if (notificationOpen) {
console.log("bg", notificationOpen);
const { title, body } = notificationOpen.notification;
this.showAlert(title, body);
}
/*
* Triggered for data only payload in foreground
* */
this.messageListener = firebase.messaging().onMessage(message => {
//process data message
console.log("fg", JSON.stringify(message));
});
}
在这里
firebase.notifications().onNotification
或
firebase.messaging().onMessage()
在其他解决方案中,这是因为在Android 8中,您需要创建一个频道,但在从FCM发送通知时,我找不到任何创建频道的选项
notification composer
.