maestri-remote/public/sw.js

59 lines
1.6 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 title = (data.notification && data.notification.title) || data.title || 'Maestri Remote';
const body = (data.notification && data.notification.body) || data.body || 'Novo prompt aguardando sua resposta';
const stationId = data.data?.stationId || data.stationId || 'default';
const options = {
body,
icon: '/icon.svg',
badge: '/icon.svg',
vibrate: [200, 100, 200, 100, 200],
data: data.data || data,
tag: `maestri-station-${stationId}`,
renotify: true,
requireInteraction: true,
actions: [
{ action: 'open', title: 'Abrir no App' }
]
};
event.waitUntil(
self.registration.showNotification(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('/');
}
})
);
});