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

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

嗨,我正在使用 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 device 因为它只在应用程序托盘中显示通知然后我检查了 settings > my_app > notifications > show floating notification turned在 然后抬头开始进入该设备,但后来我尝试使用 one plus 设备,因为它没有显示。

我检查了所有这些问题

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

最佳答案

下面是我是如何破解它的。

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

此外,您还必须在 Android 上设置 channel 并订阅该 channel 才能使其正常工作。

这是我更新后的代码。

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. 创建一个 channel 并订阅它。
  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();
  }

关于android - React Native firebase 通知抬头未在某些设备中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50875069/

相关文章:

android - 从 GCM/FCM 的生产证书添加 SHA-1

java - android fragment 使用 onCreateView 但无法添加 firebase

javascript - 检查 firebase js .forEach 方法是否完成?

安卓垂直微光

android - 在 TWebBrowser 中将图钉添加到 Google map 并处理点击

Android:以固定间隔拉取位置崩溃,Looper Prepare(是的,我搜索过!!)

javascript - "Error: Cannot modify a WriteBatch that has been committed."尝试在 firebase 函数中使用批量写入时

ios - 是否可以在 React Native 中使用 iOS 13 系统字体?

ios - 我可以在不授予日历访问权限的情况下添加日历事件吗?

react-native - React Native如何包装内容 View