React Native Firebase消息传递后台通知无法打开应用

时间:2020-07-07 12:56:59

标签: react-native react-native-firebase

我向我的app.js文件添加了Firebase推送通知,如下所示:

import messaging from '@react-native-firebase/messaging';

async function registerAppWithFCM() {
  await messaging().registerDeviceForRemoteMessages();
}

//inside app function:
const unsubscribe = messaging().onMessage(async remoteMessage => {
  console.log("Foreground message");
})

messaging().setBackgroundMessageHandler(async remoteMessage => {
  console.log('Message handled in the background!', remoteMessage);
});

然后,当应用程序在前台运行时,我从服务器发送了一条通知,该通知正常工作(记录为“前台消息”), 当我在后台应用程序中再次尝试时,通知已到达并记录下来(消息在后台处理!和远程消息),但是 单击它打开应用程序时,该应用程序崩溃了(应用程序已停止), 我正在使用:

“ @ react-native-firebase / app”:“ ^ 6.4.0”, “ @ react-native-firebase / messaging”:“ ^ 6.4.0”, “反应”:“ 16.11.0”, “ react-native”:“ 0.62.0”,

1 个答案:

答案 0 :(得分:0)

messaging().onNotificationOpenedApp(remoteMessage => {
      console.log(
        'Notification caused app to open from background state:',
        remoteMessage.notification,
      );
      navigation.navigate(remoteMessage.data.type);
    });

    // Check whether an initial notification is available
    messaging()
      .getInitialNotification()
      .then(remoteMessage => {
        if (remoteMessage) {
          console.log(
            'Notification caused app to open from quit state:',
            remoteMessage.notification,
          );
          setInitialRoute(remoteMessage.data.type); // e.g. "Settings"
        }
        setLoading(false);
      });

使用此通知侦听器

https://rnfirebase.io/messaging/notifications