我正在向我的应用发送多个通知。我要实现的是,每当用户单击一个通知,然后消失在通知托盘中的所有通知时。
我尝试添加
notification.android.setAutoCancel(true)
仅对一个通知(被点击的通知)起作用的方法
我也尝试过:
firebase.notifications().removeAllDeliveredNotifications()
没有任何效果。
我该如何实现?
这是我的完整代码:
componentDidMount() {
firebase.notifications().removeAllDeliveredNotifications()
this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification) => {
});
this.notificationListener = firebase.notifications().onNotification(async (notification) => {
// Process your notification as required
notification.android.setAutoCancel(true)
firebase.notifications().removeAllDeliveredNotifications()
}
async componentWillMount() {
this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification) => {
});
this.notificationListener = firebase.notifications().onNotification((notification) => {
});
this.notificationDisplayedListener();
this.notificationListener();
}
答案 0 :(得分:3)
尝试移动代码以删除通知
(removeAllDeliveredNotifications()
从onNotification
侦听器到onNotificationOpened
侦听器。您尝试删除到达通知时,可能存在竞争状况。
这也是因为您希望在用户点击一个通知时清除通知。
PS。将通知监听器放在componentDidMount
或componentWillMount
中,而不要同时放在两者中。