All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
47 lines
1.6 KiB
JavaScript
47 lines
1.6 KiB
JavaScript
// Service Worker pro Web Push notifikace (připomínka výběru oběda)
|
|
|
|
self.addEventListener('push', (event) => {
|
|
const data = event.data?.json() ?? { title: 'Luncher', body: 'Ještě nemáte zvolený oběd!' };
|
|
event.waitUntil(
|
|
self.registration.showNotification(data.title, {
|
|
body: data.body,
|
|
icon: '/favicon.ico',
|
|
tag: 'lunch-reminder',
|
|
actions: [
|
|
{ action: 'neobedvam', title: 'Mám vlastní/neobědvám' },
|
|
],
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close();
|
|
|
|
if (event.action === 'neobedvam') {
|
|
event.waitUntil(
|
|
self.registration.pushManager.getSubscription().then((subscription) => {
|
|
if (!subscription) return;
|
|
return fetch('/api/notifications/push/quickChoice', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ endpoint: subscription.endpoint }),
|
|
});
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
event.waitUntil(
|
|
self.clients.matchAll({ type: 'window' }).then((clientList) => {
|
|
// Pokud je již otevřené okno, zaostříme na něj
|
|
for (const client of clientList) {
|
|
if (client.url.includes(self.location.origin) && 'focus' in client) {
|
|
return client.focus();
|
|
}
|
|
}
|
|
// Jinak otevřeme nové
|
|
return self.clients.openWindow('/');
|
|
})
|
|
);
|
|
});
|