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

时间:2019-11-24 09:12:39

标签: react-native react-native-firebase

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

我在下面共享代码,

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"
  },

2 个答案:

答案 0 :(得分:1)

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

  

现在,您必须使用HeadlessJS功能,该功能在   反应原生。

您可以在这里查阅Firebase的官方文档:-https://rnfirebase.io/docs/v5.x.x/messaging/receiving-messages#4)-(Optional)(Android-only)-Listen-for-FCM-messages-in-the-background

希望这会有所帮助。...谢谢:)

答案 1 :(得分:0)

@ abhikumar22答案有一个新网址:

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

相关问题