51 lines
1.2 KiB
JavaScript
51 lines
1.2 KiB
JavaScript
const CACHE_NAME = 'agency-cache-v1';
|
|
const ASSETS = [
|
|
'/',
|
|
'/index.html',
|
|
'/src/main.js',
|
|
'/src/style.css',
|
|
'/manifest.json',
|
|
'/icon-192.png',
|
|
'/icon-512.png'
|
|
];
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => {
|
|
return cache.addAll(ASSETS);
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
event.respondWith(
|
|
caches.match(event.request).then((response) => {
|
|
return response || fetch(event.request);
|
|
})
|
|
);
|
|
});
|
|
self.addEventListener('push', (event) => {
|
|
const data = event.data ? event.data.json() : { title: 'Notification', body: 'New update from Agency' };
|
|
|
|
const options = {
|
|
body: data.body,
|
|
icon: '/icon-192.png',
|
|
badge: '/icon-192.png',
|
|
vibrate: [100, 50, 100],
|
|
data: {
|
|
dateOfArrival: Date.now(),
|
|
primaryKey: '1'
|
|
}
|
|
};
|
|
|
|
event.waitUntil(
|
|
self.registration.showNotification(data.title, options)
|
|
);
|
|
});
|
|
|
|
self.addEventListener('notificationclick', (event) => {
|
|
event.notification.close();
|
|
event.waitUntil(
|
|
clients.openWindow('/')
|
|
);
|
|
});
|