Add web push notifications

This commit is contained in:
hbrain 2026-05-26 15:48:30 +02:00
parent 91cb65e896
commit 4e4def843d
5 changed files with 92 additions and 3 deletions

25
sw.js Normal file
View file

@ -0,0 +1,25 @@
self.addEventListener('push', event => {
let data = {};
try { data = event.data ? event.data.json() : {}; } catch (e) { data = { title: 'MVLog', body: event.data ? event.data.text() : 'New video published' }; }
const title = data.title || 'MVLog';
const options = {
body: data.body || 'New video published',
icon: 'assets/img/moto_travel.png',
badge: 'assets/img/moto_travel.png',
data: { url: data.url || './' },
tag: data.tag || 'mvlog-new-video',
renotify: true
};
event.waitUntil(self.registration.showNotification(title, options));
});
self.addEventListener('notificationclick', event => {
event.notification.close();
const url = event.notification.data && event.notification.data.url ? event.notification.data.url : './';
event.waitUntil(clients.matchAll({ type: 'window', includeUncontrolled: true }).then(clientList => {
for (const client of clientList) {
if (client.url === url && 'focus' in client) return client.focus();
}
return clients.openWindow(url);
}));
});