25 lines
807 B
JavaScript
25 lines
807 B
JavaScript
self.addEventListener('push', (event) => {
|
|
const data = event.data.json();
|
|
event.waitUntil(
|
|
self.registration.showNotification(data.title, {
|
|
body: data.body,
|
|
icon: data.icon || '/icons/trophy-192.png',
|
|
badge: '/icons/badge-72.png',
|
|
data: data.data,
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close();
|
|
const url = event.notification.data?.url || '/';
|
|
event.waitUntil(
|
|
clients.matchAll({ type: 'window' }).then((clientList) => {
|
|
for (const client of clientList) {
|
|
if (client.url.includes(url) && 'focus' in client) return client.focus();
|
|
}
|
|
if (clients.openWindow) return clients.openWindow(url);
|
|
})
|
|
);
|
|
});
|