34 lines
980 B
JavaScript
34 lines
980 B
JavaScript
self.addEventListener('install', () => self.skipWaiting());
|
|
self.addEventListener('activate', (e) => e.waitUntil(self.clients.claim()));
|
|
|
|
self.addEventListener('push', (e) => {
|
|
let data = { title: '🔎 Research Agent', body: 'Eine Recherche ist fertig!' };
|
|
try {
|
|
data = JSON.parse(e.data.text());
|
|
} catch {}
|
|
e.waitUntil(
|
|
self.registration.showNotification(data.title, {
|
|
body: data.body,
|
|
icon: '/icons/icon-192.png',
|
|
badge: '/icons/icon-192.png',
|
|
tag: 'research-done',
|
|
data: { url: data.url || '/' },
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('notificationclick', (e) => {
|
|
e.notification.close();
|
|
const url = e.notification.data?.url || '/';
|
|
e.waitUntil(
|
|
self.clients
|
|
.matchAll({ type: 'window', includeUncontrolled: true })
|
|
.then((clients) => {
|
|
for (const client of clients) {
|
|
if ('focus' in client) return client.focus();
|
|
}
|
|
return self.clients.openWindow(url);
|
|
})
|
|
);
|
|
});
|