代码之家  ›  专栏  ›  技术社区  ›  Abdul Rafay

React Native-无法在Android中访问前台的Firebase推送通知

  •  0
  • Abdul Rafay  · 技术社区  · 7 年前

    React Native: Integrating Push Notifications using FCM

    async createNotificationListeners() {
      /*
       * Triggered when a particular notification has been received in foreground
       * */
      this.notificationListener = firebase
        .notifications()
        .onNotification(notification => {
          console.log("fg", notification);
          const { title, body } = notification;
          this.showAlert(title, body);
        });
    
      /*
       * If app is in background, listen for when a notification is clicked / tapped / opened as follows:
       * */
      this.notificationOpenedListener = firebase
        .notifications()
        .onNotificationOpened(notificationOpen => {
          console.log("bg", notificationOpen);
          const { title, body } = notificationOpen.notification;
          this.showAlert(title, body);
        });
    
      /*
       * If app is closed, check if it was opened by a notification being clicked / tapped / opened as follows:
       * */
      const notificationOpen = await firebase
        .notifications()
        .getInitialNotification();
      if (notificationOpen) {
        console.log("bg", notificationOpen);
        const { title, body } = notificationOpen.notification;
        this.showAlert(title, body);
      }
      /*
       * Triggered for data only payload in foreground
       * */
      this.messageListener = firebase.messaging().onMessage(message => {
        //process data message
        console.log("fg", JSON.stringify(message));
      });
    }
    

    在这里 firebase.notifications().onNotification firebase.messaging().onMessage()

    在其他解决方案中,这是因为在Android 8中,您需要创建一个频道,但在从FCM发送通知时,我找不到任何创建频道的选项 notification composer .

    0 回复  |  直到 7 年前
        1
  •  1
  •   ajthyng    7 年前

    阿卜杜勒,

    您需要在设备上使用firebase创建通知通道。

    const channel = new firebase.notifications.Android
      .Channel('default', 'Default Channel', firebase.notifications.Android.Importance.Max)
      .setDescription('The default notification channel.')
    
    firebase.notifications().android.createChannel(channel)
    

      notification.android.setChannelId('default')