我正在尝试通过遵循本文来实现用firebase Console App中的React Native中的推送通知:
React Native:使用FCM
集成推送通知在Android中,当应用在后台但无法在前景中接收时,我可以收到通知。使用此方法接收通知:
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发送通知时找不到任何选项。
abdul,
您需要使用设备上的firebase进行通知通道。
const channel = new firebase.notifications.Android
.Channel('default', 'Default Channel', firebase.notifications.Android.Importance.Max)
.setDescription('The default notification channel.')
firebase.notifications().android.createChannel(channel)
这将创建频道,您可以按照自己的安全方式称呼它多次(根据文档)。您的通知侦听器看起来不错,您只需要在包含的通知上设置Android频道。
notification.android.setChannelId('default')