fix browser notifications + make them work when browser is closed

This commit is contained in:
unknown
2026-03-23 20:24:35 +01:00
parent 17b88cb160
commit c33f6d4108
10 changed files with 480 additions and 36 deletions
@@ -18,9 +18,7 @@ self.addEventListener("notificationclick", (event) => {
try {
const clientUrl = new URL(client.url);
if (clientUrl.origin === self.location.origin) {
if (clientUrl.pathname !== "/notifications") {
client.navigate(targetUrl);
}
client.navigate(targetUrl);
return client.focus();
}
} catch {}
@@ -30,6 +28,29 @@ self.addEventListener("notificationclick", (event) => {
);
});
self.addEventListener("push", (event) => {
if (!event.data) return;
let data;
try {
data = event.data.json();
} catch {
data = { title: "Overlord", body: event.data.text() };
}
const title = String(data.title || "Overlord Notification");
const options = {
body: String(data.body || ""),
icon: "/assets/overlord.png",
badge: "/assets/overlord.png",
tag: data.tag || `overlord-push-${Date.now()}`,
data: { url: data.url || "/notifications" },
requireInteraction: false,
};
event.waitUntil(self.registration.showNotification(title, options));
});
self.addEventListener("message", (event) => {
const data = event.data;
if (!data || data.type !== "show_notification") return;
@@ -40,7 +61,7 @@ self.addEventListener("message", (event) => {
icon: data.icon || "/assets/overlord.png",
badge: "/assets/overlord.png",
tag: data.tag || `overlord-${Date.now()}`,
data: { url: "/notifications" },
data: { url: data.url || "/notifications" },
requireInteraction: false,
};