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

问题描述 投票:2回答:1

我正在尝试通过以下文章在React Native中使用Firebase控制台应用程序实现推送通知:

React Native: Integrating Push Notifications using FCM

在Android中,当应用程序处于后台但无法在前台接收时,我能够收到通知。使用此方法接收通知:

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().onNotificationfirebase.messaging().onMessage()根本没有被触发。

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

android firebase react-native push-notification firebase-cloud-messaging
1个回答
1
投票

阿卜杜勒

您需要在设备上使用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)

这将创建频道,您可以根据需要安全地调用它(根据文档)。你的onNotification监听器看起来很好,你只需要在进来的通知上设置android通道。

  notification.android.setChannelId('default')
© www.soinside.com 2019 - 2024. All rights reserved.