【问题标题】:How to Take Action / Move Screen When FCM Notification is Opened?打开 FCM 通知时如何采取行动/移动屏幕?
【发布时间】:2020-02-01 08:31:01
【问题描述】:

我目前正在使用 FCM React Native Firebase,我想在打开 fcm 通知时移动屏幕或采取行动,如下图所示:

【问题讨论】:

  • rnfirebase 的哪个版本?
  • "react-native-firebase": "5.5.5"
  • 因为我有点困惑,你给@GauravRoy上过教程吗?
  • 查看这篇文章一次:rn-deep linking希望对您有所帮助

标签: javascript firebase react-native firebase-cloud-messaging react-native-fcm


【解决方案1】:

react-native-fcm 现在已弃用且不再维护。最好转到 react-native-firebase。

下面显示了 react-native-firebase 的示例。

首先,您需要在您的应用程序中创建一个通知侦听器。大多数情况下,最好将其添加到顶级根组件中。

您可以在通知负载数据对象上传递相关数据。 https://firebase.google.com/docs/cloud-messaging/concept-options

import firebase from 'react-native-firebase';

componentDidMount() {
      this.createNotificationListeners();
}

componentWillUnmount() {
    // Remove all the notification related listeners on unmounting the main dashboard
    if (this.notificationOpenedListener) {
      this.notificationOpenedListener();
    }
}

  /**
   * Contains all the listeners related to the Firebase notification services.
   */
  async createNotificationListeners() {
    const handleNotification = notificationOpen => {
      // Do what ever do you want, based on your notification payload
    };

    /*
     * If app is in background, listen for when a notification is clicked / tapped / opened as follows:
     * */
    try {
      this.notificationOpenedListener = firebase
        .notifications()
        .onNotificationOpened(notificationOpen => {
          console.log(
            'FirebaseDataReceiver remote notification clicked from background :',
            notificationOpen,
          );
          handleNotification(notificationOpen);
        });
    } catch (e) {
      console.log(
        'Error while clicking the remote notification from background :',
        e,
      );
    }

    /*
     * If app is closed, check if it was opened by a notification being clicked / tapped / opened as follows:
     * */
    try {
      const notificationOpen = await firebase
        .notifications()
        .getInitialNotification();
      if (notificationOpen) {
        console.log(
          'FirebaseDataReceiver remote notification clicked app start up :',
          notificationOpen,
        );
        handleNotification(notificationOpen);
      }
    } catch (e) {
      console.log(
        'Error while clicking the app was initiated by a remote notification :',
        e,
      );
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-14
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 2011-12-16
    • 2020-05-05
    相关资源
    最近更新 更多