【问题标题】:React Native: managing notification on RNFirebaseReact Native:管理 RNFirebase 上的通知
【发布时间】:2019-02-23 07:26:30
【问题描述】:

我已经使用react-native-firebase 库成功实现了基本通知功能,一切都按预期工作,信息已正确接收并准备用于我尚未确定的目的。我的通知处理部分的代码目前看起来像这样:

 componentDidMount() {
        /**
         * When app on foreground, rewrap received notification and re-send it as notification using channelId
         * A workaround because channelId never set by default by FCM API so we need to rewrap to make sure it is
         * shown on user's notification tray
         */ 
        this.notificationListener = firebase.notifications().onNotification((notification) => {
            //data object must have channelId props as a workaround for foreground notification on Android
            console.log('Notif ', notification);
            notification.android.setChannelId(notification.data.channelId);
            firebase.notifications().displayNotification(notification);
        });

        //On Notification tapped, be it from foreground or background
        this.notificationOpen = firebase.notifications().onNotificationOpened((notificationOpen) => {
            //body and title lost if accessed from background, taking info from data object by default
            const notification = notificationOpen.notification;
            console.log('Open ', notification)
            Alert.alert(notification.data.title, notification.data.body);
        });

        //When notification received when app is closed
        this.initialNotification = firebase.notifications().getInitialNotification()
            .then((notificationOpen) => {
                //body and title lost if accessed this way, taking info from data object where info will persist
                if (notificationOpen) {
                    const notification = notificationOpen.notification;
                    console.log('Initial ', notification)
                    Alert.alert(notification.data.title, notification.data.body);
                }
            });
    }

    componentWillUnmount() {
        this.notificationListener();
        this.initialNotification()
        this.notificationOpen();
    }

上面的代码让我可以使用我从firebase控制台或我的同事在上述范围内设置的php服务器发送的任何信息(不确定服务器端的实现是如何完成的,但它给了我完全相同的通知对象在我这边)。

这很好,但问题是当我从 Firebase 控制台在 IOS 上设置徽章时,一旦我打开通知,徽章就不会消失。 我一直试图弄清楚是否有任何额外的位我必须添加到上面的块中以编程方式减少徽章计数器,但到目前为止还没有运气。

因此,如果这里有人可以向我展示如何正确管理这些通知对象(特别是解释这些对象的性质和生命周期 - 即哪些属性/方法上的哪些数据在通知对象的范围内持续存在或者是静态的) Android 和 IOS,将不胜感激 :)

【问题讨论】:

    标签: firebase react-native push-notification firebase-cloud-messaging react-native-firebase


    【解决方案1】:

    结果是根 componentDidMount() 上的简单 firebase.notifications().setBadge(0) 会在应用打开时清除徽章计数。

    也可能需要使用firebase.notifications().removeAllDeliveredNotifications()firebase.notifications().cancelAllNotifications() 将它们从通知托盘中删除。

    【讨论】:

      【解决方案2】:

      可能您必须在创建通知时为徽章设置代码

        this.notificationListener = firebase.notifications().onNotification((notification) => {
      
           const localNotification = new firebase.notifications.Notification()
                      .setNotificationId(notification.notificationId)
                      .setTitle(notification.title)
                      .setSubtitle(notification.subtitle)
                      .setBody(notification.body)
                      .setData(notification.data)
                      .ios.setBadge(notification.ios.badge);
      
                  firebase.notifications()
                      .displayNotification(localNotification)
                      .catch(err => console.error(err));
       }
      

      在构建通知时将此行放入代码.ios.setBadge(notification.ios.badge);,然后重试

      【讨论】:

      • 那不是在通知显示之前设置的位吗?我的问题实际上是在打开后将其删除..
      • 您认为在app.js 中执行firebase.notifications().removeAllDeliveredNotifications() 会奏效吗?其背后的逻辑是,只要用户打开了应用程序,徽章和通知对象就应该从托盘中消失
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      相关资源
      最近更新 更多