2020-05-05 18:40:56 +00:00
|
|
|
/* eslint-disable no-restricted-globals, no-console */
|
|
|
|
/* globals clients */
|
|
|
|
self.addEventListener('push', event => {
|
|
|
|
let notification = event.data && event.data.json();
|
2019-10-18 07:07:09 +00:00
|
|
|
|
2020-05-05 18:40:56 +00:00
|
|
|
event.waitUntil(
|
|
|
|
self.registration.showNotification(notification.title, {
|
|
|
|
tag: notification.tag,
|
|
|
|
data: {
|
|
|
|
url: notification.url,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2019-10-18 07:07:09 +00:00
|
|
|
});
|
|
|
|
|
2020-05-05 18:40:56 +00:00
|
|
|
self.addEventListener('notificationclick', event => {
|
|
|
|
let notification = event.notification;
|
|
|
|
|
|
|
|
event.waitUntil(
|
|
|
|
clients.matchAll({ type: 'window' }).then(windowClients => {
|
|
|
|
let matchingWindowClients = windowClients.filter(
|
|
|
|
client => client.url === notification.data.url
|
|
|
|
);
|
|
|
|
|
|
|
|
if (matchingWindowClients.length) {
|
|
|
|
let firstWindow = matchingWindowClients[0];
|
|
|
|
if (firstWindow && 'focus' in firstWindow) {
|
|
|
|
firstWindow.focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (clients.openWindow) {
|
|
|
|
clients.openWindow(notification.data.url);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|