// Service Worker for Quiet Thanks PWA const CACHE_NAME = 'quiet-thanks-v1'; // Install event - cache essential assets self.addEventListener('install', (event) => { console.log('[SW] Installing service worker...'); self.skipWaiting(); }); // Activate event - clean up old caches self.addEventListener('activate', (event) => { console.log('[SW] Activating service worker...'); event.waitUntil(clients.claim()); }); // Push event - handle incoming push notifications self.addEventListener('push', (event) => { console.log('[SW] Push event received'); let data = { title: 'Quiet Thanks', body: 'Take a moment to reflect on what you\'re grateful for today.', icon: '/icons/icon.svg', badge: '/icons/icon.svg', tag: 'daily-reminder', }; if (event.data) { try { const payload = event.data.json(); data = { ...data, ...payload }; } catch (e) { data.body = event.data.text(); } } const options = { body: data.body, icon: data.icon, badge: data.badge, tag: data.tag, vibrate: [100, 50, 100], data: { url: '/', }, actions: [ { action: 'open', title: 'Open app' }, { action: 'dismiss', title: 'Dismiss' }, ], }; event.waitUntil( self.registration.showNotification(data.title, options) ); }); // Notification click handler self.addEventListener('notificationclick', (event) => { console.log('[SW] Notification clicked'); event.notification.close(); if (event.action === 'dismiss') { return; } event.waitUntil( clients.matchAll({ type: 'window', includeUncontrolled: true }) .then((clientList) => { // Focus existing window if available for (const client of clientList) { if (client.url.includes(self.registration.scope) && 'focus' in client) { return client.focus(); } } // Open new window if (clients.openWindow) { return clients.openWindow('/'); } }) ); }); // Periodic sync for background reminders (when supported) self.addEventListener('periodicsync', (event) => { if (event.tag === 'daily-reminder') { event.waitUntil(checkAndShowReminder()); } }); async function checkAndShowReminder() { // This would check the reminder time and show notification if appropriate // For now, we rely on the client-side scheduler console.log('[SW] Periodic sync triggered'); }