65 lines
1.4 KiB
JavaScript
65 lines
1.4 KiB
JavaScript
const DEFAULT_ICON = '/favicon.svg'
|
|
|
|
self.addEventListener('install', () => {
|
|
self.skipWaiting()
|
|
})
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(self.clients.claim())
|
|
})
|
|
|
|
self.addEventListener('push', (event) => {
|
|
let payload = {}
|
|
|
|
try {
|
|
payload = event.data ? event.data.json() : {}
|
|
} catch {
|
|
payload = {
|
|
title: 'Neue TEG-Nachricht',
|
|
body: event.data ? event.data.text() : '',
|
|
url: '/chat',
|
|
}
|
|
}
|
|
|
|
const title = payload.title || 'Neue TEG-Nachricht'
|
|
const icon = payload.icon || DEFAULT_ICON
|
|
|
|
event.waitUntil(
|
|
self.registration.showNotification(title, {
|
|
body: payload.body || '',
|
|
icon,
|
|
badge: payload.badge || DEFAULT_ICON,
|
|
tag: payload.tag || 'teg-message',
|
|
renotify: true,
|
|
requireInteraction: true,
|
|
data: {
|
|
url: payload.url || '/chat',
|
|
},
|
|
}),
|
|
)
|
|
})
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close()
|
|
|
|
const targetURL = new URL(
|
|
event.notification.data?.url || '/chat',
|
|
self.location.origin,
|
|
).href
|
|
|
|
event.waitUntil(
|
|
self.clients
|
|
.matchAll({ type: 'window', includeUncontrolled: true })
|
|
.then(async (windowClients) => {
|
|
for (const client of windowClients) {
|
|
if ('navigate' in client) {
|
|
await client.navigate(targetURL)
|
|
}
|
|
return client.focus()
|
|
}
|
|
|
|
return self.clients.openWindow(targetURL)
|
|
}),
|
|
)
|
|
})
|