POST /api/internal/push/test-send: requireAdmin check (redirect bij niet-admin), optioneel body met defaults, roept sendPushToUser aan, 204. public/sw.js: push-handler met showNotification, notificationclick met same-origin guard, focus bestaand venster of openWindow. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
// Service Worker for Web Push notifications (PBI-55)
|
|
|
|
self.addEventListener('push', (event) => {
|
|
let payload = { title: 'Scrum4Me', body: '', url: '/', tag: undefined }
|
|
try {
|
|
if (event.data) payload = { ...payload, ...event.data.json() }
|
|
} catch (_) {}
|
|
|
|
event.waitUntil(
|
|
self.registration.showNotification(payload.title, {
|
|
body: payload.body,
|
|
icon: '/icon-192.png',
|
|
badge: '/icon-192.png',
|
|
tag: payload.tag,
|
|
data: { url: payload.url },
|
|
})
|
|
)
|
|
})
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close()
|
|
|
|
const rawUrl = event.notification.data?.url || '/'
|
|
const targetUrl = new URL(rawUrl, self.location.origin)
|
|
|
|
// Same-origin guard
|
|
if (targetUrl.origin !== self.location.origin) return
|
|
|
|
event.waitUntil(
|
|
self.clients
|
|
.matchAll({ type: 'window', includeUncontrolled: true })
|
|
.then((clients) => {
|
|
for (const client of clients) {
|
|
if (client.url.startsWith(self.location.origin) && 'focus' in client) {
|
|
client.navigate(targetUrl.href)
|
|
return client.focus()
|
|
}
|
|
}
|
|
return self.clients.openWindow(targetUrl.href)
|
|
})
|
|
)
|
|
})
|