Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notifications - Clear all notifications in the android notification tray

Tags:

I'm sending multiple notifications to my app. What I want to achieve is whenever a user clicks one notification then all notifications in the notification tray disspear.

I've tried adding

notification.android.setAutoCancel(true)

which does the trick for only one notification (the one which is being clicked)

I've also tried:

firebase.notifications().removeAllDeliveredNotifications() 

which doesn't have any effect.

How can I achieve this?

Here's my full code:

      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();
    }
like image 907
Dan Dinu Avatar asked May 15 '19 20:05

Dan Dinu


1 Answers

Try moving the code for removing notifications (removeAllDeliveredNotifications()) from onNotification listener to onNotificationOpened listener. There might be a race condition as you are trying to remove notifications on arrival.

Also because you want to clear notifications when user taps on one notification.

PS. Keep the notification listeners in either componentDidMount or componentWillMount, not both.

like image 81
Nishant Nair Avatar answered Nov 03 '22 01:11

Nishant Nair