feat: pwa notifications

This commit is contained in:
2026-07-14 20:49:30 +02:00
parent 2cf7b283e0
commit 5198826f83
21 changed files with 1563 additions and 8 deletions
+39
View File
@@ -16,6 +16,45 @@ self.addEventListener("activate", (event) => {
);
});
self.addEventListener("push", (event) => {
let payload = {};
try {
payload = event.data ? event.data.json() : {};
} catch (e) {
payload = { title: "Osiris", body: event.data ? event.data.text() : "" };
}
event.waitUntil(
self.registration.showNotification(payload.title || "Osiris", {
body: payload.body || "",
icon: "/static/icons/icon-192.png",
badge: "/static/icons/icon-192.png",
// Same tag replaces the previous notification instead of stacking.
tag: payload.tag || "osiris",
renotify: true,
requireInteraction: true, // A dose reminder should wait, not fade away.
data: { url: payload.url || "/admin/today/" },
})
);
});
self.addEventListener("notificationclick", (event) => {
event.notification.close();
const url = (event.notification.data && event.notification.data.url) || "/admin/today/";
// Focus the app if it is already open rather than opening a second window.
event.waitUntil(
self.clients
.matchAll({ type: "window", includeUncontrolled: true })
.then((windows) => {
for (const client of windows) {
if (client.url.includes(url) && "focus" in client) return client.focus();
}
return self.clients.openWindow(url);
})
);
});
self.addEventListener("fetch", (event) => {
const { request } = event;