diff options
Diffstat (limited to 'site/public/firebase-messaging-sw.js')
| -rw-r--r-- | site/public/firebase-messaging-sw.js | 118 |
1 files changed, 23 insertions, 95 deletions
diff --git a/site/public/firebase-messaging-sw.js b/site/public/firebase-messaging-sw.js index 056ce2b..225652c 100644 --- a/site/public/firebase-messaging-sw.js +++ b/site/public/firebase-messaging-sw.js @@ -12,114 +12,54 @@ firebase.initializeApp({ const messaging = firebase.messaging(); -// Handle background messages -messaging.onBackgroundMessage(function(payload) { - console.log('[firebase-messaging-sw.js] Received background message', payload); - - // Extract notification data - const notificationTitle = payload.notification?.title || 'New Update'; - const notificationBody = payload.notification?.body || 'You have a new notification'; - - // Build notification options with enhanced features - const notificationOptions = { - body: notificationBody, - icon: payload.notification?.icon || '/android/android-launchericon-192-192.png', - badge: '/android/android-launchericon-72-72.png', - vibrate: [200, 100, 200], - tag: payload.data?.tag || 'default-tag', - requireInteraction: payload.data?.requireInteraction === 'true', - renotify: true, - silent: false, - timestamp: Date.now(), - data: { - url: payload.data?.url || '/', - gameId: payload.data?.gameId, - ...payload.data - } - }; - - // Add image if provided - if (payload.notification?.image) { - notificationOptions.image = payload.notification.image; - } - - // Add actions if provided - if (payload.data?.actions) { - try { - notificationOptions.actions = JSON.parse(payload.data.actions); - } catch (e) { - console.error('Failed to parse notification actions:', e); - } - } - - // Show the notification - return self.registration.showNotification(notificationTitle, notificationOptions); -}); - // Handle notification clicks self.addEventListener('notificationclick', function(event) { console.log('[firebase-messaging-sw.js] Notification click received.', event); - event.notification.close(); - // Handle action clicks + const clickUrl = event.notification.data?.url || '/'; + if (event.action) { - console.log('Action clicked:', event.action); - // You can handle different actions here if (event.action === 'view') { - event.waitUntil( - clients.openWindow(event.notification.data?.url || '/') - ); - } else if (event.action === 'dismiss') { - // Just close the notification - return; + event.waitUntil(clients.openWindow(clickUrl)); } - } else { - // Default click behavior - open the URL - const clickUrl = event.notification.data?.url || '/'; - - event.waitUntil( - clients.matchAll({ - type: 'window', - includeUncontrolled: true - }).then(function(clientList) { - // Check if there's already a window/tab open with the target URL - for (const client of clientList) { - if (client.url === clickUrl && 'focus' in client) { - return client.focus(); - } - } - // If no existing window/tab, open a new one - if (clients.openWindow) { - return clients.openWindow(clickUrl); - } - }) - ); + return; } + + // Default click behavior + event.waitUntil( + clients.matchAll({ type: 'window', includeUncontrolled: true }).then(clientList => { + for (const client of clientList) { + if (client.url === clickUrl && 'focus' in client) { + return client.focus(); + } + } + if (clients.openWindow) { + return clients.openWindow(clickUrl); + } + }) + ); }); // Handle notification close self.addEventListener('notificationclose', function(event) { console.log('[firebase-messaging-sw.js] Notification was closed', event); - // You can track notification dismissals here if needed }); -// Handle service worker installation -self.addEventListener('install', function(event) { +// Install & activate +self.addEventListener('install', event => { console.log('[firebase-messaging-sw.js] Service Worker installing.'); self.skipWaiting(); }); -// Handle service worker activation -self.addEventListener('activate', function(event) { +self.addEventListener('activate', event => { console.log('[firebase-messaging-sw.js] Service Worker activated.'); event.waitUntil(clients.claim()); }); -// Handle push events (for debugging) -self.addEventListener('push', function(event) { +// Debugging push events +self.addEventListener('push', event => { console.log('[firebase-messaging-sw.js] Push event received', event); - if (event.data) { try { const data = event.data.json(); @@ -129,15 +69,3 @@ self.addEventListener('push', function(event) { } } }); - -// Error handling -self.addEventListener('error', function(event) { - console.error('[firebase-messaging-sw.js] Service Worker error:', event); -}); - -// Fetch event handler for offline support (optional) -self.addEventListener('fetch', function(event) { - // You can add offline caching strategies here if needed - // For now, just pass through the request - return; -});
\ No newline at end of file |
