5

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();
    }
Dan Dinu
  • 32,492
  • 24
  • 78
  • 114

1 Answers1

4

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.

Nishant Nair
  • 1,837
  • 13
  • 19
  • awesome thanks! I've tired removing that several times (not having the listeners in 2 places) but then notifications stop working for some reason :| – Dan Dinu Jun 09 '19 at 19:54
  • Hey, I have the same issue can you check this out please, @NishantNair https://stackoverflow.com/questions/57171787/navigate-to-screen-after-opening-a-notification/57181291 – DevAS Jul 24 '19 at 14:21