React Native:在RNFirebase上管理通知

时间:2018-09-19 07:30:36

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

我已经成功使用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,都将不胜感激:)

2 个答案:

答案 0 :(得分:2)

在根firebase.notifications().setBadge(0)上显示一个简单的componentDidMount(),可在每次打开应用程序时清除徽章计数。

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

答案 1 :(得分:0)

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

  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);中,然后重试