This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
'use strict';
|
||||
|
||||
self.addEventListener('push', (event) => {
|
||||
let payload = {
|
||||
title: 'Dota lobby update',
|
||||
body: 'The monitored lobby changed.',
|
||||
url: '/admin',
|
||||
tag: 'doka-lobby',
|
||||
};
|
||||
|
||||
try {
|
||||
if (event.data) payload = { ...payload, ...event.data.json() };
|
||||
} catch {
|
||||
if (event.data) payload.body = event.data.text();
|
||||
}
|
||||
|
||||
event.waitUntil(self.registration.showNotification(payload.title, {
|
||||
body: payload.body,
|
||||
tag: payload.tag,
|
||||
data: { url: payload.url || '/admin' },
|
||||
requireInteraction: true,
|
||||
}));
|
||||
});
|
||||
|
||||
self.addEventListener('notificationclick', (event) => {
|
||||
event.notification.close();
|
||||
const target = new URL(event.notification.data?.url || '/admin', self.location.origin).href;
|
||||
event.waitUntil((async () => {
|
||||
const windows = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
|
||||
const existing = windows.find((client) => client.url.startsWith(self.location.origin));
|
||||
if (existing) {
|
||||
await existing.navigate(target);
|
||||
return existing.focus();
|
||||
}
|
||||
return self.clients.openWindow(target);
|
||||
})());
|
||||
});
|
||||
Reference in New Issue
Block a user