maestri-remote/public/sw.js

52 lines
1.2 KiB
JavaScript

const CACHE_NAME = 'maestri-remote-v1';
self.addEventListener('install', (event) => {
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(self.clients.claim());
});
self.addEventListener('push', (event) => {
let data = { title: 'Maestri Remote', body: 'Novo evento aguardando resposta' };
try {
if (event.data) {
data = event.data.json();
}
} catch (err) {
if (event.data) data.body = event.data.text();
}
const options = {
body: data.body,
icon: '/icon.svg',
badge: '/icon.svg',
vibrate: [100, 50, 100],
data: data.data || {},
actions: [
{ action: 'open', title: 'Abrir no App' }
]
};
event.waitUntil(
self.registration.showNotification(data.title, options)
);
});
self.addEventListener('notificationclick', (event) => {
event.notification.close();
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => {
for (const client of clientList) {
if (client.url && 'focus' in client) {
return client.focus();
}
}
if (clients.openWindow) {
return clients.openWindow('/');
}
})
);
});