Firebase通知无法在Android上打开应用程序

时间:2020-05-11 13:46:27

标签: android react-native firebase-cloud-messaging react-native-firebase

在使用Firebase云消息传递的react-native应用程序上,我希望收到有关所有应用程序状态(前景,背景,关闭)的推送通知,因此我遵循this answer并显示了通知。

然后按通知我想重定向到关于通知对象中数据的特定路由。当应用程序在前台时,它可以工作。

但是;

  • 当应用处于后台或关闭状态时,按通知不会打开该应用。
  • 当应用程序为前台时,按下通知即可,但通知不会消失,而是保留在通知栏中(或称为通知栏)。

请注意以下代码;

  • componentWillUnmountappState更改为background时,所有侦听器均被删除。
  • 在添加前台支持之前,按通知打开应用程序

    应用中的

    个版本: “ react”:“ 16.8.3”, “ react-native”:“ 0.59.10”, “ react-native-firebase”:“ 5.2.0”,

代码是

async componentDidMount(){await setupFirebase()}

private async setupFirebase(): Promise<void> {

    const enabled = await firebase.messaging().hasPermission();
    if (enabled) {
      /** some code */
    } else {
      // user doesn't have permission
      try {
        await firebase.messaging().requestPermission();
      } catch (error) {
        console.log(error);
      }
    }

    if (Platform.OS === 'android') {
      // Build a channel
      const channel = new firebase.notifications.Android.Channel(
        'foreground_notifications',
        'Foreground Notifications',
        firebase.notifications.Android.Importance.Max,
      ).setDescription('Channel to receive notifications when app is active');
      firebase.notifications().android.createChannel(channel);
    }

    this.removeNotificationListener = firebase.notifications().onNotification((ntf: Notification) => {
      if (Platform.OS === 'android') {
        const localNotification = new firebase.notifications.Notification()
          .setNotificationId(ntf.notificationId)
          .setTitle(ntf.title)
          .setSubtitle(ntf.subtitle)
          .setBody(ntf.body)
          .setData(ntf.data)
          .android.setChannelId('foreground_notifications')
          .android.setPriority(firebase.notifications.Android.Priority.High);
        firebase
          .notifications()
          .displayNotification(localNotification)
          .catch((err) => console.error('Foreground notification error!', err));
      } else if (Platform.OS === 'ios') {
        const localNotification = new firebase.notifications.Notification()
          .setNotificationId(ntf.notificationId)
          .setTitle(ntf.title)
          .setSubtitle(ntf.subtitle)
          .setBody(ntf.body)
          .setData(ntf.data)
          .ios.setBadge(ntf.ios.badge);
        firebase
          .notifications()
          .displayNotification(localNotification)
          .catch((err) => console.error('Foreground notification error!', err));
        console.log('IOS foreground notification with local notification DONE!');
      } else {
        Alert.alert();
      }
    });
    // If your app is closed, you can check if it was
    // opened by a notification being clicked / tapped / opened as follows:
    const notificationOpen: NotificationOpen = await firebase.notifications().getInitialNotification();
    if (notificationOpen) {
      const {doc_id} = notificationOpen.notification.data
      this.navigationToTheID(doc_id);
    }
    // If your app is in the foreground, or background, you can
    // listen for when a notification is clicked / tapped / opened as follows:
    this.removeNotificationOpenedListener = firebase
      .notifications()
      .onNotificationOpened((ntfOpened: NotificationOpen) => {
        console.log('onNotificationOpened', notificationOpen);
        this.navigationToNotification(ntfOpened);
      });

    this.removeOnNotificationDisplayed = firebase.notifications().onNotificationDisplayed((notification) => {
      console.log(`%c onNotification displayed: `, 'color:green;', notification);
    });
  }
  /**
    These functions are called on unmount and/or apostate change to background
    removeNotificationListener()
    removeNotificationOpenedListener()
    removeOnNotificationDisplayed()
  */

0 个答案:

没有答案