0

I am implementing Push Notifications for the first time and exploring all of the features I can find, however I haven't been able to find anything about retracting or closing notifications.

I know that there must be some method of achieving this because when I receive a Facebook notification, if I open it on my desktop then it disappears from my mobile automatically.

Does anybody know how this functionality can be achieved?

Josh McGee
  • 443
  • 6
  • 16

1 Answers1

0

The trick to my question is knowing the difference between a Push and a Notification. From what I can tell the Push is the method used to send the data, the Notification is the visualisation in the status bar (for mobile). The Push can't be unsent, however the Notification can be closed.

Here is some information about the Notification API and its close() method

Using a combination of the answers here and here, the following code will remove all notifications from a PWA when the app is opened

navigator.serviceWorker.ready.then(function(registration) {
    registration.getNotifications().then(function(notifications) {
        for (let i = 0; i < notifications.length; i += 1) {
            notifications[i].close();
        }
    }) 
});
Josh McGee
  • 443
  • 6
  • 16