RN FireBase 推送通知,关闭应用程序时无法访问通知标题和正文 (getInitialNotification)



下面的代码工作正常,除了当我关闭运行并单击下一个传入通知时,没有标题和正文。

我在下面分享代码,与

async createNotificationListeners() {
/*
* Triggered when a particular notification has been received in foreground
* */
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { title, body } = notification;
console.log('onNotification:');
const localNotification = new firebase.notifications.Notification({
sound: 'sampleaudio',
show_in_foreground: true,
})
...
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
const { title, body } = notificationOpen.notification;
console.log('onNotificationOpened:');
Alert.alert(title, body)
});

const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
debugger
const { title, body } = notificationOpen.notification; // title and body undefined
const { data } = notificationOpen.notification;// and data has only google.delivered_priority ..
console.log('getInitialNotification:');
Alert.alert(title, body)
}
}

如何通过单击传入通知打开应用程序来获取通知内容,

我的应用程序包 JSON 喜欢;

"dependencies": {
"react": "16.9.0",
"react-native": "0.61.5",
"react-native-firebase": "^5.5.6"
},

当应用程序关闭时,您的侦听器将无法在 react 本机中工作,而不是仅在应用程序处于前台和后台时才工作。因此,对于应用程序关闭时的状态,您必须在 react 本机应用程序中添加后台任务。

现在你必须使用HeadlessJS功能,它在 反应原生。

您可以在此处参考Firebase官方文档:- https://rnfirebase.io/docs/v5.x.x/messaging/receiving-messages#4(-(可选((仅限Android(-Listen-for-FCM-messages-in-the-background

希望这有帮助....谢谢:)

有一个新的 Url 用于@abhikumar22答案:

https://v5.rnfirebase.io/docs/v5.x.x/messaging/receiving-messages#4(-(可选((仅限Android(-侦听FCM消息-在后台

相关内容

  • 没有找到相关文章

最新更新