56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
self.addEventListener("install", event => {
|
|
event.waitUntil(self.skipWaiting());
|
|
});
|
|
|
|
self.addEventListener("activate", event => {
|
|
event.waitUntil(self.clients.claim());
|
|
});
|
|
|
|
self.addEventListener("push", event => {
|
|
let payload = {};
|
|
|
|
try {
|
|
payload = event.data ? event.data.json() : {};
|
|
} catch (error) {
|
|
payload = {};
|
|
}
|
|
|
|
const title = payload.title || "Mood-Board";
|
|
const options = {
|
|
body: payload.body || "Zeit für deinen heutigen Eintrag.",
|
|
icon: payload.icon || "/assets/branding/logo-mark.svg",
|
|
badge: payload.badge || "/assets/branding/favicon.svg",
|
|
tag: payload.tag || "mood-reminder",
|
|
data: {
|
|
url: payload.url || "/track",
|
|
},
|
|
};
|
|
|
|
event.waitUntil(self.registration.showNotification(title, options));
|
|
});
|
|
|
|
self.addEventListener("notificationclick", event => {
|
|
event.notification.close();
|
|
|
|
const targetUrl = event.notification.data?.url || "/track";
|
|
|
|
event.waitUntil((async () => {
|
|
const clients = await self.clients.matchAll({
|
|
type: "window",
|
|
includeUncontrolled: true,
|
|
});
|
|
|
|
for (const client of clients) {
|
|
const clientUrl = new URL(client.url);
|
|
const target = new URL(targetUrl, self.location.origin);
|
|
|
|
if (clientUrl.pathname === target.pathname) {
|
|
await client.focus();
|
|
return;
|
|
}
|
|
}
|
|
|
|
await self.clients.openWindow(targetUrl);
|
|
})());
|
|
});
|