Files
Avtex/apps/web/app/plugins/push-notifications.client.ts
T

27 lines
923 B
TypeScript

export default defineNuxtPlugin(() => {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
console.log('[PWA] Service Worker ready:', registration.scope)
})
// Handle push messages when app is open
navigator.serviceWorker.addEventListener('message', event => {
if (event.data?.type === 'PUSH_RECEIVED') {
const { title, body } = event.data.payload || {}
// Local alarm: vibrate and show notification
if (navigator.vibrate) {
navigator.vibrate([200, 100, 200])
}
if ('Notification' in window && Notification.permission === 'granted') {
new Notification(title || 'XFlow Alarm', {
body: body || 'Alarm triggered',
icon: '/pwa-192x192.svg',
badge: '/favicon.svg',
...({ vibrate: [200, 100, 200] } as any)
})
}
}
})
}
})