// Service Worker for Zixflow Web Push Notifications
self.addEventListener('install', (event) => {
console.log('[SW] Installing service worker')
self.skipWaiting()
})
self.addEventListener('activate', (event) => {
console.log('[SW] Activating service worker')
event.waitUntil(self.clients.claim())
})
self.addEventListener('push', (event) => {
console.log('[SW] Push event received')
if (!event.data) {
console.warn('[SW] Push event has no data')
return
}
let data
try {
data = event.data.json()
} catch (e) {
console.error('[SW] Failed to parse push data:', e)
return
}
const title = data.title || 'Notification'
const options = {
body: data.body || '',
icon: data.icon || '/icon.png',
badge: data.badge || '/badge.png',
image: data.image,
data: data,
tag: data.tag || 'zixflow-push',
requireInteraction: data.requireInteraction || false,
actions: data.actions || []
}
event.waitUntil(
self.registration.showNotification(title, options)
)
})
self.addEventListener('notificationclick', (event) => {
console.log('[SW] Notification clicked:', event.action)
event.notification.close()
const clickedUrl = event.action
? event.notification.data.actions?.[event.action]?.url
: event.notification.data.url || '/'
event.waitUntil(
clients.openWindow(clickedUrl).then((windowClient) => {
if (windowClient) {
windowClient.focus()
}
})
)
})