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

How to show push notifications in foreground with react-native-firebase v6?

我在从 React Native firebase v5 迁移到 v6 时遇到了几个问题。 v6 中似乎没有 notifications() 方法。如何使用 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)。 messaging() 方法不包含与 v5 不同的通知。

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

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