25 lines
1 KiB
JavaScript
25 lines
1 KiB
JavaScript
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);
|
|
}));
|
|
});
|