React Native firebase 通知抬头未在某些设备中显示

React native firebase notification heads up are not showing in some devices

你好,我正在使用 react native firebase 通知,我成功地集成了它,两个平台都会收到通知,但是 android 提醒不会来 当应用程序在前台或后台。我阅读了所有与此相关的问题,但没有任何线索。

app environment:

所以当应用程序在前台时,应用程序需要处理通知,这就是我正在做的:

componentDidMount() {
    this.checkFirebase();
  }

  registerFbCloudMessagingListener = () => {
    firebase.notifications().onNotification(notification => {
      if (Platform.OS === "android") {
        notification.android.setChannelId("forgroundnotification");
      }
      firebase.notifications().displayNotification(notification);
    });
  };
  async checkFirebase() {
    const enabled = await firebase.messaging().hasPermission();
    if (enabled) {
      // user has permissions
      this.registerFbCloudMessagingListener();
    } else {
      // user doesn't have permission
      this.requestFbPermission();
    }
  }

  async requestFbPermission() {
    try {
      let permission = await firebase.messaging().requestPermission();
      if (permission) {
        this.checkFirebase();
      }
      // User has authorised
    } catch (error) {
      // User has rejected permissions
    }
  }

开始我使用的是 mi 设备 因为它只在应用程序托盘中显示通知然后我签入 settings > my_app > notifications > show floating notification turned on 然后注意开始出现在该设备中,但后来我尝试使用 one plus 设备,因为它没有显示。

我检查了所有这些问题

In oreo its not showing i think. because mi is having android N. Please help !!! Advance thanks.

下面是我是如何破解的。

首先,从 firebase 控制台推送通知不会在 android 上显示通知。这个东西我是从discord频道得到的。在那里我问了这个问题,有人建议设置自己的服务器,比如使用 firebase API 从后端服务器触发通知,然后它开始工作。

此外,您还必须在 android 上设置频道并订阅该频道才能正常工作。

这是我更新后的代码。

Note this code is based on

"react-native-firebase": "4.2.0"

"react": "16.3.1"

"react-native": "0.55.3"

很多方法在最新版本中有可能发生变化,我提供的代码仅供参考。

当时我确实遵循了以下步骤:

 async checkFirebase() {
    const enabled = await firebase.messaging().hasPermission();
    if (enabled) {
      // user has permissions
      this.registerFbCloudMessagingListener();
    } else {
      // user doesn't have permission
      this.requestFbPermission();
    }
  }
async requestFbPermission() {
    try {
      let permission = await firebase.messaging().requestPermission();
      if (permission) {
        this.checkFirebase();
      }
      // User has authorised
    } catch (error) {
      // User has rejected permissions
    }
  }
const channelConfig = {
  channelId: "channelId",
  channelName: "Channel Name"
};

  1. 订阅主题
  2. 创建一个频道并订阅它。
  3. 检查权限,如果没有则请求一个。
 componentDidMount() {
    firebase.messaging().subscribeToTopic("test");
    const channel = new firebase.notifications.Android.Channel(
      channelConfig.channelId,
      channelConfig.channelName,
      firebase.notifications.Android.Importance.Max
    ).setDescription("A natural description of the channel");
    firebase.notifications().android.createChannel(channel);
    this.checkFirebase();
  }