【问题标题】:React native firebase notification heads up are not showing in some devices在某些设备中未显示 React 本机 Firebase 通知提示
【发布时间】:2018-11-25 06:27:40
【问题描述】:

您好,我正在使用react native firebase 进行通知,我成功集成了它,并且两个平台的通知都将到来,但是当应用程序处于前台或后台时,android head up are not come。我阅读了有关此的所有问题,但没有任何线索。

应用环境:

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

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 然后抬头开始进入该设备,但后来我尝试使用 一加设备,因为它没有显示。

我检查了所有这些问题

oreo 我认为它没有显示出来。因为 mi 有 android N。 请帮忙 !!!提前谢谢。

【问题讨论】:

  • 你解决了这个问题吗?我也一样
  • 抱歉回复晚了,我会尽快分享我是怎么做到的。但这可能需要一些时间,因为我很忙。 @普拉文
  • 嘿!我遇到了类似的问题,很想看看它是如何解决的。
  • 抱歉延迟回复我已经添加了答案,请检查是否有帮助。

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


【解决方案1】:

这是我如何破解它的。

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

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

这是我更新的代码。

请注意,此代码基于

“react-native-firebase”:“4.2.0”

“反应”:“16.3.1”

“反应原生”:“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();
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多