React-native FCM:当应用程序处于后台或被杀死时无法访问通知数据

问题描述 投票:0回答:2

我正在尝试通过 Firebase 云消息传递和 React-Native 应用程序使工作成为 React-Native-FCM。

我主要关注Android

[email protected]
[email protected]
OS: Android - Emulator - 7.0.2

当我的应用程序位于前台时,它运行良好,我在此函数中接收数据

FCM.on(FCMEvent.Notification, async (notif) => {

我可以处理一切

image

但是当应用程序在后台时,会收到通知,但是当我点击横幅时,应用程序会转到前台,会触发相同的功能,但 notif 对象不同,并且不包含我的数据。

image

当应用程序被杀死时,也会收到通知,但是当我点击横幅时,应用程序启动,

FCM.getInitialNotification()
被触发,并且我的 notif 对象也不包含数据,与后台的行为相同

image

我正在使用 firebase Web 界面的 node-gcm 库测试消息的格式,但没有任何效果。

这是我的消息示例,但我尝试了很多组合:

let message = new gcm.Message({
    priority: 'high',
    content_available: true,
    timeToLive: 3,
    data: {
        key1: 'message1',
        key2: 'message2'
    },
    notification: {
        title: "Hello, World",
        icon: "ic_launcher",
        body: "This is a notification that will be displayed if your app is in the background."
    },
    custom_notification: {
        key1: 'message1',
        key2: 'message2'
    }

});

在这些情况下我应该如何继续发送和接收数据?因为让我发疯。

如果您需要更多数据,请索取:)

谢谢!!

react-native push-notification firebase-cloud-messaging android-notifications react-native-fcm
2个回答
3
投票

就我而言,在前台通知事件 firebase.notifications().onNotification 上构建通知时,我缺少通知的数据设置

new firebase.notifications.Notification()
      .setNotificationId(fcmNotification.data.notificacionId)
      .setTitle(fcmNotification.title)
      .setBody(fcmNotification.body)
      .setData(fcmNotification.data) // <- this little bastard

因此,当点击通知时,

data
firebase.notifications().onNotificationOpened
上未定义,即使传入的json
firebase.notifications().onNotification
已填充数据字段


0
投票

问题:我从messaging().getInitialNotification()获取remoteData,而messaging().setBackgroundHandler()也没有注册。 解决方案:@react-native-firebase,@react-native-firebase/messaging与@notifee冲突我只需删除notifee模块,它对我有用..

© www.soinside.com 2019 - 2024. All rights reserved.