23 lines
819 B
JavaScript
23 lines
819 B
JavaScript
self.addEventListener("push", (event) => {
|
|
if (!event.data) return;
|
|
const message = event.data.json();
|
|
event.waitUntil(self.registration.showNotification(message.title, {
|
|
body: message.body,
|
|
icon: message.icon || "/icon.png",
|
|
tag: message.id,
|
|
data: { url: message.url },
|
|
}));
|
|
});
|
|
|
|
self.addEventListener("notificationclick", (event) => {
|
|
event.notification.close();
|
|
const url = new URL(event.notification.data.url, self.location.origin);
|
|
if (url.origin !== self.location.origin) return;
|
|
event.waitUntil((async () => {
|
|
const windows = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
|
|
const existing = windows.find((client) => client.url === url.href);
|
|
if (existing) return existing.focus();
|
|
return self.clients.openWindow(url.href);
|
|
})());
|
|
});
|