【问题标题】:Is there any call back that hit when we receive firebase notification in background in react native app?当我们在 React Native 应用程序的后台收到 Firebase 通知时,是否有任何回调?
【发布时间】:2020-01-16 10:27:07
【问题描述】:

我已经对面向 IOS 和 android 的本机应用程序做出反应。我在我的 react 原生移动应用程序中集成了 firebase 通知。当应用程序在后台时,我遇到了 android 的问题,然后没有回调被击中,但从 firebase 的托盘中收到通知。

我想在应用程序处于后台时添加通知计数逻辑,以便当用户打开应用程序时,他可以看到有 2 个或类似的新通知。还想更新徽章计数。

我知道如何更新徽章计数器或通知计数器,就像我在应用程序处于后台时所做的那样,然后会触发 onNotification 回调。但是当应用程序在后台时收到新通知时,我想点击一些代码。

通知代码

      async componentWillUnmount() {
        await this.notificationListener(); 
        await this.notificationOpenedListener();
        await this.messageListener();
      }

     messageListener = async () => {
     // When Notification is Recived and app in fore ground
     this.notificationListener = firebase
      .notifications()      
      .onNotification(async notification => {
        debugger
        console.log("on notification");
        const { title, body } = notification;
        const badgeCount = await firebase.notifications().getBadge();
        debugger
        firebase.notifications().setBadge(badgeCount+1);
        await this.props.clearNotificationCount(badgeCount+1).then(res => {
         debugger
          BadgeAndroid.setBadge(this.props.notifications.Count)
         console.log("BadgeCount is" + badgeCount);
       });

      });

     this.notificationOpenedListener = firebase
     .notifications()
      .onNotificationOpened(async notificationOpen => {
       debugger
       console.log("on open listner");
       const { title, body } = notificationOpen.notification;
       const badgeCount = await firebase.notifications().getBadge();
       debugger
       this.props.setNotificationCount(badgeCount).then(res => {
        //  BadgeAndroid.setBadge(badgeCount)
       });
        firebase.notifications().setBadge(0);
        });
         const notificationOpen = await firebase
       .notifications()
       .getInitialNotification();

      if (notificationOpen) {
       console.log("Notification open");
       console.log(notificationOpen.notification);
       const { title, body } = notificationOpen.notification;
        }
        this.messageListener = firebase.messaging().onMessage(message => {
        console.log("on message listner");
          // firebase.notifications().setBadge(2);
       console.log(JSON.stringify(message));
        });
          };

     checkPermission = async () => {
      const enabled = await firebase.messaging().hasPermission();
     if (enabled) {
       await this.getFcmToken();
         } else {
       await this.requestPermission();
       }
      };

       getFcmToken = async () => {
        const fcmToken = await firebase.messaging().getToken();
         if (fcmToken) {
          let SaveFirebaseTokenEndpoint = ApiEndPoint.SavefireBasetoken;
         let myResponse = await DataAccess.PostSecured(SaveFirebaseTokenEndpoint, {
                DeviceToken: fcmToken
              });
              console.log(fcmToken);
              console.log(myResponse);
            } else {
              //  this.showAlert('Failed', 'No token received');
            }
          };

          requestPermission = async () => {
            try {
              await firebase.messaging().requestPermission();
              // User has authorised
            } catch (error) {
              // User has rejected permissions
            }
          };

onNotificationOpened() 永远不会在后台点击

【问题讨论】:

    标签: android firebase react-native push-notification


    【解决方案1】:

    在 Android 上,您可以实现 headless JS 以在您的应用位于 background 时侦听通知。请注意,这仅适用于 data-only Firebase 消息。

    在 Android 设备上也存在一个已知问题,特别是带有修改过的操作系统的中国设备,它具有积极的电池保护程序。你可以阅读更多here。

    在 iOS 上,当应用处于后台/关闭状态时,您无法显示通知。

    【讨论】:

    • 当我从 FCM 发送通知 + 数据消息时。当应用程序在后台使用 android 中的 Headless js 时也可以接收它们吗?
    • 没有。允许data-only 消息。您需要手动显示通知。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    相关资源
    最近更新 更多