android - React-Native-Firebase Cloud Messaging-如果从任务栏打开则没有事件

标签 android ios firebase firebase-cloud-messaging react-native-fcm

我使用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进入前台后切换到此屏幕)

最佳答案

将您的听众更改为这样的内容。

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将捕获通知数据。

关于android - React-Native-Firebase Cloud Messaging-如果从任务栏打开则没有事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50425347/

相关文章:

android - 如何将数据更改为来自服务器的不同语言?

iphone - 在 "viewDidLoad"方法期间发出的 UIActionSheet 提示是否应该暂停方法中的代码,直到它被用户操作?

ios - Objective-C 中的字符串比较与 isEqualToString

android - 在循环中添加 post 参数 - OkHTTP

Android fastboot oem 解锁

ios - 模糊自动布局约束(以编程方式添加)

firebase - 如何在firebase firestore中准确合并多个流

javascript - 来自 Google 身份验证提供程序的 Firebase photoURL 返回颜色反转的 jpg

firebase - 您如何搜索/访问用户数据?

Java 不在 TextView 中打印字符串