// sw.js self.addEventListener("push", function (event) { const data = event.data ? event.data.json() : {}; const title = data.title || "Shit da ist was falsch"; // title braucht man, sonst Error // Dynamisch Options-Objekt nur mit vorhandenen Werten const options = {}; if (data.body) options.body = data.body; if (data.icon) options.icon = data.icon; if (data.badge) options.badge = data.badge; if (data.actions) options.actions = data.actions; if (data.requireInteraction !== undefined) options.requireInteraction = data.requireInteraction; if (data.renotify !== undefined) options.renotify = data.renotify; if (data.tag) options.tag = data.tag; if (data.vibrate) options.vibrate = data.vibrate; if (data.url) options.data = { url: data.url }; event.waitUntil( self.registration.showNotification(title, options) ); }); self.addEventListener("notificationclick", function (event) { event.notification.close(); // Notification schließen // Prüfen, ob eine Action gedrückt wurde if (event.action) { // Dynamische Aktionen vom Server können hier behandelt werden // Beispiel: Wir öffnen die URL, die im Notification-Data-Feld steht if (event.notification.data && event.notification.data.url) { event.waitUntil( clients.openWindow(event.notification.data.url) ); } // Optional: Weitere Action-IDs können hier behandelt werden console.log("Action clicked:", event.action); } else { // Notification selbst angeklickt (ohne Button) if (event.notification.data && event.notification.data.url) { event.waitUntil( clients.openWindow(event.notification.data.url) ); } } }); self.addEventListener('install', event => { console.log("Service Worker installiert"); // Sofort aktivieren, ohne auf alte Version zu warten self.skipWaiting(); }); self.addEventListener('activate', event => { console.log("Service Worker aktiviert"); // Alte Clients übernehmen event.waitUntil(self.clients.claim()); });