self.addEventListener('push', (event) => { console.log('[SW] push received', event.data?.text()); if (!event.data) return; let title = 'HealthyFit'; let body = 'You have a new notification'; let url = '/'; try { const data = event.data.json(); title = data.title; body = data.body; url = data.url ?? '/'; } catch { body = event.data.text(); } event.waitUntil( self.registration.showNotification(title, { body, icon: '/icons/icon-192.png', badge: '/icons/icon-192.png', data: { url }, }) ); }); self.addEventListener('notificationclick', (event) => { event.notification.close(); const url = event.notification.data?.url ?? '/'; event.waitUntil( self.clients .matchAll({ type: 'window', includeUncontrolled: true }) .then((clients) => { const existing = clients.find((c) => c.url.includes(url)); if (existing) return existing.focus(); return self.clients.openWindow(url); }) ); });