self.addEventListener('push', event => { let data = { title: 'Eira', body: 'You have a new notification', icon: '/icons/eira-icon.png', badge: '/icons/eira-badge.png', data: { url: '/' // default fallback } }; if (event.data) { try { const json = event.data.json(); data.title = json.title || data.title; data.body = json.body || data.body; data.icon = json.icon || data.icon; data.badge = json.badge || data.badge; data.data.url = json.data.url || data.data.url; } catch (e) { console.error('Error parsing push event data:', e); } } event.waitUntil(self.registration.showNotification(data.title, { body: data.body, icon: data.icon, badge: data.badge, data: { url: data.data.url } })); }); self.addEventListener('notificationclick', event => { event.notification.close(); const urlToOpen = event.notification.data.url || '/'; event.waitUntil(clients.matchAll({ type: 'window', includeUncontrolled: true }) .then(windowClients => { for (const client of windowClients) { if (client.url === urlToOpen && 'focus' in client) { return client.focus(); } } if (clients.openWindow) { return clients.openWindow(urlToOpen); } })); });