首页 > 解决方案 > React-Native - Firebase 云消息传递 - 如果从托盘打开则没有事件

问题描述

我使用 fcm-package ( https://github.com/evollu/react-native-fcm ) 将 Firebase-Cloud-Messages 发送到 android。

消息到达电话。

如果应用程序处于后台,则通知会出现在状态栏中。如果我点击消息,应用程序会在主屏幕上打开 - 在我的情况下是 News_Screen。

但是在应用程序出现后,我没有找到一种方法来捕获通知的数据。

FCM.on(FCMEvent.Notification, (notif) => {不要得到任何事件。

如果应用程序在前台,并且我发送通知,则console.log-output 工作。

如果应用程序从后台到前台,我是否会错过一些特别的东西才能获取事件数据?

这是我的应用程序的结构:

index.js ⇒ /app/index.js ⇒ (imports a StackNavigator)

StackNavigator 的默认屏幕是“News_Screen”。

在新闻屏幕中,我有这个:

async componentDidMount() {

        // START Firebase Cloud Messaging
        try {
            let result = await FCM.requestPermissions({
                badge: false,
                sound: true,
                alert: true
            });
        } catch (e) {
            console.error(e);
        }

        FCM.getFCMToken().then(token => {
            this.setState({token: token || ""});
            console.log("First the token: ", token)
            // store fcm token in your server
        });
        if (Platform.OS === "ios") {
            FCM.getAPNSToken().then(token => {
                console.log("APNS TOKEN (getFCMToken)", token);
            });
        }

FCM.on(FCMEvent.Notification, (notif) => {
            if(notif.opened_from_tray){
                setTimeout(()=>{
                    console.log("notif.opened_from_tray", notif);
                }, 1000)
            }

            // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
            console.log("Message erhalten", notif);
            if (notif.screen !== undefined) {
                console.log("Jo, der Screen is", notif.screen, this.props.navigation);
                this.props.navigation.navigate('Menu_Screen');
            }
        });
        FCM.on(FCMEvent.RefreshToken, (token) => {
            console.log("Again the token:", token)
            // fcm token may not be available on first load, catch it here
        });
    }

如果我推送通知并且应用程序从后台到前台,我得到的唯一 console.log 是First the token...消息。

如何捕获通知的数据。(例如,如果 Notification 具有附加的 Object-Parmete: screen: 'Menu_Screen,则在 App 进入前台后切换到此屏幕)

标签: androidiosfirebasefirebase-cloud-messagingreact-native-fcm

解决方案


将您的听众更改为这样的东西..

FCM.on(FCMEvent.Notification, notif => {
      console.log("Notification", notif);

      if(Platform.OS ==='ios' && notif._notificationType === NotificationType.WillPresent && !notif.local_notification){
        notif.finish(WillPresentNotificationResult.All)
        return;
      }

      if(Platform.OS ==='ios'){
        switch(notif._notificationType){
          case NotificationType.Remote:
            notif.finish(RemoteNotificationResult.NewData)
            break;
          case NotificationType.NotificationResponse:
            notif.finish();
            break;
          case NotificationType.WillPresent:
            notif.finish(WillPresentNotificationResult.All)
            break;
        }
      }

      this.showLocalNotification(notif)
    });

showLocalNotification(notif) {
  console.log('here local', notif)
    FCM.presentLocalNotification({
      title: notif.title,
      body: notif.body,
      priority: "high",
      show_in_foreground: true,
      local: true
   });
}

如果应用程序在前台,此行 NotificationType.WillPresent 将捕获通知数据。


推荐阅读