retrospring/public/service_worker.js

26 lines
750 B
JavaScript
Raw Normal View History

self.addEventListener('push', function (event) {
if (event.data) {
const notification = event.data.json();
event.waitUntil(self.registration.showNotification(notification.title, {
2022-09-11 23:55:06 +02:00
body: notification.body,
2022-12-24 21:11:30 +01:00
tag: notification.type,
2022-12-26 01:19:52 +01:00
icon: notification.icon,
}));
} else {
console.error("Push event received, but it didn't contain any data.", event);
}
});
2022-09-11 23:55:06 +02:00
self.addEventListener('notificationclick', async event => {
if (event.notification.tag === 'inbox') {
event.preventDefault();
2022-12-26 12:09:25 +01:00
return clients.openWindow("/inbox", "_blank").then(result => {
event.notification.close();
return result;
});
2022-09-11 23:55:06 +02:00
} else {
console.warn(`Unhandled notification tag: ${event.notification.tag}`);
}
});