const CACHE_NAME = 'teamchat-v2'; const STATIC_ASSETS = ['/']; self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS)) ); self.skipWaiting(); }); self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((keys) => Promise.all(keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k))) ) ); self.clients.claim(); }); self.addEventListener('fetch', (event) => { const url = event.request.url; if (url.includes('/api/') || url.includes('/socket.io/') || url.includes('/manifest.json')) { return; } event.respondWith( fetch(event.request).catch(() => caches.match(event.request)) ); }); // Track badge count in SW let badgeCount = 0; self.addEventListener('push', (event) => { if (!event.data) return; const data = event.data.json(); badgeCount++; // Update app badge (supported on Android Chrome and some desktop) if (navigator.setAppBadge) { navigator.setAppBadge(badgeCount).catch(() => {}); } event.waitUntil( self.registration.showNotification(data.title || 'New Message', { body: data.body || '', icon: '/icons/icon-192.png', badge: '/icons/icon-192.png', data: { url: data.url || '/' }, tag: 'teamchat-message', // replaces previous notification instead of stacking renotify: true, // still vibrate/sound even if replacing }) ); }); self.addEventListener('notificationclick', (event) => { event.notification.close(); badgeCount = 0; if (navigator.clearAppBadge) navigator.clearAppBadge().catch(() => {}); event.waitUntil( clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => { const url = event.notification.data?.url || '/'; for (const client of clientList) { if (client.url.includes(self.location.origin) && 'focus' in client) { client.focus(); return; } } return clients.openWindow(url); }) ); }); // Clear badge when user opens the app self.addEventListener('message', (event) => { if (event.data?.type === 'CLEAR_BADGE') { badgeCount = 0; if (navigator.clearAppBadge) navigator.clearAppBadge().catch(() => {}); } });