Reputation: 21
I've been looking at the Web Notification API or Desktop Notification API. I understand how it's constructed and the number of attributes and events that it supports. The notifications are meant to be separated from the browser sot that even if the browser is minimized for example the notification can still be displayed.
My question is, is there a way to link the notification with actions on the web page? In particular, I want to close all notification dialogues upon refreshing the page. How can I make that happen?
Thanks in advance.
Upvotes: 2
Views: 1955
Reputation: 2345
You don't mention a service worker but I'm going to assume you have one since you need it for the Push and Notifications APIs.
When the page loads, you could send a message to your service worker instructing it to close all notifications. You can then do that within the worker like this:
self.getNotifications().then(function(notifications){
notifications.forEach(function(notification){
notification.close()
})
})
However you can skip the message to the service worker and just do this from the page via the service worker registration as follows:
navigator.serviceWorker.getRegistration().then(function(registration){
registration.getNotifications().then(function(notifications){
notifications.forEach(function(notification){
notification.close()
})
})
})
However, note that getNotifications()
is only available from Chrome 44. Before Chrome 44, or in other browsers, I don't believe there's a way to do what you want.
Upvotes: 6