Add settings page for alert preferences
This commit is contained in:
43
public/sw.js
43
public/sw.js
@@ -1,5 +1,5 @@
|
||||
const CACHE_NAME = "wtr-shell-v3";
|
||||
const SHELL = ["/", "/offline", "/manifest.json", "/icons/icon.svg", "/icons/maskable.svg", "/icons/icon-192.png", "/icons/icon-512.png", "/icons/maskable-512.png"];
|
||||
const CACHE_NAME = "wtr-shell-v4";
|
||||
const SHELL = ["/", "/settings", "/offline", "/manifest.json", "/icons/icon.svg", "/icons/maskable.svg", "/icons/icon-192.png", "/icons/icon-512.png", "/icons/maskable-512.png"];
|
||||
|
||||
self.addEventListener("install", (event) => {
|
||||
event.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(SHELL)));
|
||||
@@ -32,3 +32,42 @@ self.addEventListener("fetch", (event) => {
|
||||
event.respondWith(fetch(event.request).catch(() => caches.match(event.request).then((response) => response || caches.match("/offline"))));
|
||||
}
|
||||
});
|
||||
|
||||
self.addEventListener("push", (event) => {
|
||||
const fallbackPayload = {
|
||||
title: "IMGW",
|
||||
body: "Nowe ostrzeżenie meteorologiczne.",
|
||||
url: "/warnings",
|
||||
};
|
||||
let payload = fallbackPayload;
|
||||
if (event.data) {
|
||||
const text = event.data.text();
|
||||
try {
|
||||
payload = { ...fallbackPayload, ...JSON.parse(text) };
|
||||
} catch {
|
||||
payload = { ...fallbackPayload, body: text };
|
||||
}
|
||||
}
|
||||
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(payload.title, {
|
||||
body: payload.body,
|
||||
icon: "/icons/icon-192.png",
|
||||
badge: "/icons/icon-192.png",
|
||||
data: { url: payload.url || "/warnings" },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("notificationclick", (event) => {
|
||||
event.notification.close();
|
||||
const targetUrl = new URL(event.notification.data?.url || "/warnings", self.location.origin).href;
|
||||
|
||||
event.waitUntil(
|
||||
self.clients.matchAll({ type: "window", includeUncontrolled: true }).then((clients) => {
|
||||
const existingClient = clients.find((client) => client.url === targetUrl);
|
||||
if (existingClient) return existingClient.focus();
|
||||
return self.clients.openWindow(targetUrl);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user