50 lines
1.4 KiB
JavaScript
50 lines
1.4 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
|
|
}
|
|
})
|
|
);
|
|
})
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close();
|
|
|
|
event.waitUntil(
|
|
clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => {
|
|
// Check if there's already a tab open with this URL
|
|
for (const client of clientList) {
|
|
if (client.url === event.notification.data.url && 'focus' in client) {
|
|
return client.focus();
|
|
}
|
|
}
|
|
// If not, open a new window
|
|
if (clients.openWindow) {
|
|
return clients.openWindow(event.notification.data.url);
|
|
}
|
|
})
|
|
);
|
|
});
|