const CACHE_NAME = 'jama-v1'; 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 scope let badgeCount = 0; self.addEventListener('push', (event) => { if (!event.data) return; let data = {}; try { data = event.data.json(); } catch (e) { return; } badgeCount++; // Update app badge if (self.navigator && self.navigator.setAppBadge) { self.navigator.setAppBadge(badgeCount).catch(() => {}); } // Check if app is currently visible — if so, skip the notification const showNotification = clients.matchAll({ type: 'window', includeUncontrolled: true, }).then((clientList) => { const appVisible = clientList.some( (c) => c.visibilityState === 'visible' ); // Still show if app is open but hidden (minimized), skip only if truly visible if (appVisible) return; return self.registration.showNotification(data.title || 'New Message', { body: data.body || '', icon: '/icons/icon-192.png', badge: '/icons/icon-192-maskable.png', data: { url: data.url || '/' }, // Use unique tag per group so notifications group by conversation tag: data.groupId ? `jama-group-${data.groupId}` : 'jama-message', renotify: true, }); }); event.waitUntil(showNotification); }); self.addEventListener('notificationclick', (event) => { event.notification.close(); badgeCount = 0; if (self.navigator && self.navigator.clearAppBadge) { self.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 app signals it self.addEventListener('message', (event) => { if (event.data?.type === 'CLEAR_BADGE') { badgeCount = 0; if (self.navigator && self.navigator.clearAppBadge) { self.navigator.clearAppBadge().catch(() => {}); } } if (event.data?.type === 'SET_BADGE') { badgeCount = event.data.count || 0; if (self.navigator && self.navigator.setAppBadge) { if (badgeCount > 0) { self.navigator.setAppBadge(badgeCount).catch(() => {}); } else { self.navigator.clearAppBadge().catch(() => {}); } } } });