首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React本机- Firebase Cloud消息传递-如果从Tray打开没有事件

React本机- Firebase Cloud消息传递-如果从Tray打开没有事件
EN

Stack Overflow用户
提问于 2018-05-19 12:33:47
回答 1查看 1.6K关注 0票数 2

我使用fcm-包(https://github.com/evollu/react-native-fcm)向android发送Firebase-Cloud-消息。

信息到达电话。

如果应用程序处于后台,通知将出现在Statusbar中。如果我点击消息,应用程序就会在主屏幕上打开--在我的例子中,这就是News_Screen。

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

FCM.on(FCMEvent.Notification, (notif) => {没有任何活动。

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

如果应用程序从背景到前台,我会错过一些特殊的东西来获取事件数据吗?

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

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

StackNavigator的默认屏幕是"News_Screen“。

在新闻屏幕上我有这样的说法:

代码语言:javascript
复制
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...消息。

如何捕获通知的数据。(例如,如果通知作为附加对象-Parmete:screen: 'Menu_Screen,在AppGoto前台之后切换到此屏幕)

EN

回答 1

Stack Overflow用户

发布于 2018-05-19 13:32:25

把你的听众换成这样的人。

代码语言:javascript
复制
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将捕获通知数据。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50425347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档