通知-清除android通知栏中的所有通知

时间:2019-05-15 20:39:50

标签: react-native firebase-cloud-messaging android-notifications firebase-notifications react-native-firebase

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

我尝试添加

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();
    }

1 个答案:

答案 0 :(得分:3)

尝试移动代码以删除通知 (removeAllDeliveredNotifications()onNotification侦听器到onNotificationOpened侦听器。您尝试删除到达通知时,可能存在竞争状况。

这也是因为您希望在用户点击一个通知时清除通知。

PS。将通知监听器放在componentDidMountcomponentWillMount中,而不要同时放在两者中。