通知 - 清除 android 通知托盘中的所有通知

Dan*_*inu 5 android-notifications react-native firebase-cloud-messaging firebase-notifications react-native-firebase

我正在向我的应用程序发送多个通知。我想要实现的是每当用户单击一个通知时,通知托盘中的所有通知都会消失。

我试过添加

notification.android.setAutoCancel(true)
Run Code Online (Sandbox Code Playgroud)

它只对一个通知(被点击的那个)起作用

我也试过:

firebase.notifications().removeAllDeliveredNotifications() 
Run Code Online (Sandbox Code Playgroud)

这没有任何影响。

我怎样才能做到这一点?

这是我的完整代码:

      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();
    }
Run Code Online (Sandbox Code Playgroud)

Nis*_*air 5

尝试将用于删除通知 ( ) 的代码removeAllDeliveredNotifications()onNotification侦听器移动到onNotificationOpened侦听器。当您尝试删除到达时的通知时,可能会出现竞争条件。

还因为您想在用户点击一个通知时清除通知。

附言。将通知侦听器保留在 或 中componentDidMountcomponentWillMount而不是同时保留在两者中。