retrospring/public/service_worker.js
2023-01-02 11:50:47 +01:00

22 lines
668 B
JavaScript

self.addEventListener('push', function (event) {
if (event.data) {
const notification = event.data.json();
console.log(event.data);
event.waitUntil(self.registration.showNotification(notification.title, {
body: notification.body,
tag: notification.type
}));
} else {
console.error("Push event received, but it didn't contain any data.", event);
}
});
self.addEventListener('notificationclick', async event => {
if (event.notification.tag === 'inbox') {
event.preventDefault();
return clients.openWindow("/inbox", "_blank");
} else {
console.warn(`Unhandled notification tag: ${event.notification.tag}`);
}
});