52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
self.addEventListener('install', (event) => {
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(clients.claim());
|
|
});
|
|
|
|
self.addEventListener('push', (event) => {
|
|
let notification;
|
|
try {
|
|
notification = event.data.json();
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
|
|
event.waitUntil(
|
|
self.registration.showNotification(
|
|
notification.title, {
|
|
body: notification.body,
|
|
icon: 'assets/images/dark-logo.png',
|
|
badge: 'assets/images/dark-logo.png',
|
|
vibrate: [200, 100, 200],
|
|
requireInteraction: true,
|
|
data: {
|
|
url: notification.url || null
|
|
}
|
|
})
|
|
);
|
|
})
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close();
|
|
|
|
const url = event.notification.data && event.notification.data.url;
|
|
|
|
if (!url) return;
|
|
|
|
event.waitUntil(
|
|
clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => {
|
|
for (const client of clientList) {
|
|
if (client.url === url && 'focus' in client) {
|
|
return client.focus();
|
|
}
|
|
}
|
|
if (clients.openWindow) {
|
|
return clients.openWindow(url);
|
|
}
|
|
})
|
|
);
|
|
});
|