我正在向我的应用发送多个通知。我要实现的是,每当用户单击一个通知时,通知托盘中的所有通知都会消失。
我尝试添加
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();
}
尝试移动代码以删除通知(removeAllDeliveredNotifications()
(从onNotification
侦听器到onNotificationOpened
侦听器。当您试图在到达时删除通知时,可能会有种族条件。
也是因为您想在用户点击一个通知时清除通知。
ps。将通知听众保留在componentDidMount
或componentWillMount
中,而不是两者。