La propriété ServiceWorkerGlobalScope.onnotificationclick
est un gestionnaire d'événements appelé chaque fois que l'événement notificationclick
est envoyé sur l'objet ServiceWorkerGlobalScope
, c'est-à-dire lorsqu'un utilisateur clique sur une notification affichée générée par ServiceWorkerRegistration.showNotification()
.
Les notifications créées sur le thread principal ou dans un worker qui ne sont pas des service worker utilisant le constructeur Notification()
recevront à la place un événement click
sur l'objet Notification
lui-même.
Note: Essayer de créer une notification dans le ServiceWorkerGlobalScope
en utilisant le constructeur Notification()
générera une erreur.
Syntaxe
ServiceWorkerGlobalScope.onnotificationclick = function(NotificationEvent) { ... }
ServiceWorkerGlobalScope.onnotificationclick = (NotificationEvent) => { ... }
Exemple
self.onnotificationclick = (event) => {
console.log('On notification click: ', event.notification.tag)
event.notification.close()
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(
clients
.matchAll({
type: 'window'
})
.then((clientList) => {
for (let i = 0; i < clientList.length; i++) {
const client = clientList[i]
if (client.url == '/' && 'focus' in client) return client.focus()
}
if (clients.openWindow) return clients.openWindow('/')
})
)
}
Spécifications
Spécification | État | Commentaire |
---|---|---|
Notifications API La définition de 'onnotificationclick' dans cette spécification. |
Standard évolutif | Définition initiale. Cette propriété est spécifiée dans Notifications_API même si elle fait partie de ServiceWorkerGlobalScope . |
Compatibilité des navigateurs
BCD tables only load in the browser