const CACHE_NAME = 'hotelsync-v1' self.addEventListener('install', () => self.skipWaiting()) self.addEventListener('activate', event => event.waitUntil(self.clients.claim())) self.addEventListener('push', event => { if (!event.data) return let data try { data = event.data.json() } catch { data = { title: 'HotelSync', body: event.data.text() } } const title = data.title ?? 'HotelSync' const options = { body: data.body ?? '', icon: data.icon ?? '/icons/icon-192.png', badge: '/icons/icon-72.png', tag: data.tag, renotify: !!data.tag, data: { url: data.url ?? '/' }, vibrate: [200, 100, 200], } event.waitUntil(self.registration.showNotification(title, options)) }) 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(self.location.origin)) if (existing) { existing.focus(); existing.navigate(url) } else self.clients.openWindow(url) }) ) })