Web-Push mit VAPID und Erinnerungs-Endpoint

This commit is contained in:
OpenCode 2026-11-13 01:03:54 +01:00
parent 67a1614081
commit b50267e4f0
24 changed files with 1703 additions and 50 deletions

View file

@ -6,7 +6,7 @@
* zwischengespeichert, damit niemand veraltete Buchungen sieht.
*/
const VERSION = 'dic-v2';
const VERSION = 'dic-v3';
const CACHE = VERSION;
const BASE = new URL(self.registration.scope).pathname.replace(/\/$/, '');
@ -138,3 +138,52 @@ self.addEventListener('fetch', (event) => {
event.respondWith(cacheFirst(event, request));
}
});
/* ---------------------------------------------------------- Web-Push */
self.addEventListener('push', (event) => {
let data = {};
try {
data = event.data ? event.data.json() : {};
} catch (error) {
data = {};
}
const title = data.title || 'Drink it cold – Erinnerung';
const target = data.url || `${BASE}/reminders`;
const options = {
body: data.body || '',
icon: data.icon || `${BASE}/assets/icons/icon-192.png`,
badge: data.badge || `${BASE}/assets/icons/icon-192.png`,
tag: data.tag || 'dic-reminder',
lang: 'de',
data: { url: target },
timestamp: Date.now()
};
event.waitUntil(self.registration.showNotification(title, options));
});
self.addEventListener('notificationclick', (event) => {
event.notification.close();
const raw = (event.notification.data && event.notification.data.url) || `${BASE}/reminders`;
let target;
try {
const url = new URL(raw, self.location.origin);
target = url.origin === self.location.origin ? url.href : `${self.location.origin}${BASE}/reminders`;
} catch (error) {
target = `${self.location.origin}${BASE}/reminders`;
}
event.waitUntil(
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((list) => {
for (const client of list) {
if (new URL(client.url).origin !== self.location.origin) continue;
return client.focus().then(() => {
if ('navigate' in client) return client.navigate(target);
return client;
});
}
return self.clients.openWindow(target);
})
);
});