gpt4 book ai didi

android - React Native - 无法在 Android 的前台访问 Firebase 推送通知

转载 作者:行者123 更新时间:2023-11-29 00:55:49 25 4
gpt4 key购买 nike

我正尝试按照这篇文章在 React Native 中使用 Firebase 控制台应用程序实现推送通知:

React Native: Integrating Push Notifications using 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().onNotificationfirebase.messaging().onMessage()根本没有被触发。

在其他解决方案中,这是因为从 Android 8 开始您需要创建一个 channel ,但我在从 FCM 发送通知时找不到任何创建 channel 的选项 notification composer .

最佳答案

阿卜杜勒,

您需要在设备上使用 firebase 创建通知 channel 。

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)

这将创建 channel ,您可以安全地调用任意多次(根据文档)。你的 onNotification 监听器看起来不错,你只需要在收到的通知上设置 android channel 。

  notification.android.setChannelId('default')

关于android - React Native - 无法在 Android 的前台访问 Firebase 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54841862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com