2023-11-28 19:19:04 +00:00

63 lines
1.7 KiB
JavaScript

// @ts-nocheck
// sw.js
// self.addEventListener('install', event => {
// event.waitUntil(
// caches.open('static-cache').then(() => {
// // cache files
// })
// );
// });
let user_id = 0;
let date = undefined;
self.addEventListener('activate', event => {
// alert("Service worker activated");
console.log('Service worker activating...');
event.waitUntil(self.clients.claim());
console.log('Service worker activated');
user_id = Math.random();
console.log("Setting user id to " + user_id);
date = new Date();
setTimeout(() => {
notif();
}, 5000);
});
// self.addEventListener('fetch', event => {
// event.respondWith(
// caches.match(event.request).then(response => {
// return response || fetch(event.request);
// })
// );
// });
// Send notification after 5 seconds
function notif() {
console.log('Checking notifications...');
fetch("/notif?id=" + user_id + "&date=" + date.toUTCString()).then((res) => {
res.json().then((data) => {
if (data.length) {
for (const d of data) {
console.log(d);
self.registration.showNotification('New Notification', {
body: d.text,
});
console.log('Sent notification');
}
} else {
console.log('No new notifications');
}
});
});
// self.registration.showNotification('Test Notification', {
// body: 'This is a test notification sent by the service worker!',
// });
setTimeout(() => {
notif();
}, 5000);
}