如何使用 react-native-firebase v6 在前台显示推送通知?

时间:2021-07-28 08:29:44

标签: android reactjs firebase react-native react-native-firebase

我在从 react native firebase v5 迁移到 v6 时遇到了几个问题。 v6 中似乎没有 notification() 方法。如何使用 react native firebase v6 在前台正确显示通知?

我使用 v5 的代码有效:

    async listenToPushNots(){
            firebase.notifications().onNotificationDisplayed(notification => {
                  console.log("XXXXX onNotificationDisplayed", notification);
                });
            
                firebase.notifications().onNotification(notification => {
                 //remote notification received
                  console.log("XXXXX LISTENER onNotification", notification);
            
                  // notification.android.setChannelId(notification._notificationId);
                  // firebase.notifications().displayNotification(notification);
            
                  const localNotification = new firebase.notifications.Notification({
                    sound: "default",
                    show_in_foreground: true,
                    show_in_background: true
                  })
                    .setNotificationId(notification._notificationId)
                    .setTitle(notification._data.title)
                    .setSubtitle(notification._data.body)
                    .setBody(notification._data.body)
                    .setData(notification._data)
                    .android.setChannelId("my-app-channel")
                    .android.setSmallIcon("ic_stat_ic_notif") // create this icon in Android Studio
                    .android.setLargeIcon("ic_launcher_round")
                    .android.setColor("#000000") // you can set a color here
                    .android.setPriority(firebase.notifications.Android.Priority.High);
            
                  firebase
                    .notifications()
                    .displayNotification(localNotification)
                    .catch(err => console.error(err));
            }
        
        async componentDidMount(){
         listenToPushNots();
        }

在 v6 中如何替换 firebase.notifications()?他们的官方文档中没有关于如何正确迁移 (https://rnfirebase.io/messaging/notifications) 的参考。与 v5 不同,messages() 方法不包含通知。

1 个答案:

答案 0 :(得分:0)

正如您从他们的文档 (https://rnfirebase.io/messaging/usage) 中看到的,您不能将 react-native-firebase v6 用于前台通知:

enter image description here

添加前台通知的最佳方式是添加https://github.com/zo0r/react-native-push-notification