This commit is contained in:
the-djmaze 2023-03-14 10:46:52 +01:00
parent b824c394ab
commit 2201060888

View file

@ -22,15 +22,19 @@ let DesktopNotifications = false,
WorkerNotifications = navigator.serviceWorker; WorkerNotifications = navigator.serviceWorker;
// Are Notifications supported in the service worker? // Are Notifications supported in the service worker?
if (WorkerNotifications && ServiceWorkerRegistration && ServiceWorkerRegistration.prototype.showNotification) { if (WorkerNotifications) {
/* Listen for close requests from the ServiceWorker */ if (ServiceWorkerRegistration && ServiceWorkerRegistration.prototype.showNotification) {
WorkerNotifications.addEventListener('message', event => { /* Listen for close requests from the ServiceWorker */
const obj = JSON.parse(event.data); WorkerNotifications.addEventListener('message', event => {
'notificationclick' === obj?.action && dispatchMessage(obj.data); const obj = JSON.parse(event.data);
}); 'notificationclick' === obj?.action && dispatchMessage(obj.data);
});
} else {
console.log('ServiceWorkerRegistration.showNotification undefined');
WorkerNotifications = null;
}
} else { } else {
WorkerNotifications = null; console.log('ServiceWorker undefined');
console.log('ServiceWorker Notifications not supported');
} }
export const NotificationUserStore = new class { export const NotificationUserStore = new class {
@ -65,7 +69,7 @@ export const NotificationUserStore = new class {
} }
if (WorkerNotifications) { if (WorkerNotifications) {
// Service-Worker-Allowed HTTP header to allow the scope. // Service-Worker-Allowed HTTP header to allow the scope.
WorkerNotifications.register('/serviceworker.js') WorkerNotifications.register('./serviceworker.js')
// WorkerNotifications.register(Links.staticLink('js/serviceworker.js'), {scope:'/'}) // WorkerNotifications.register(Links.staticLink('js/serviceworker.js'), {scope:'/'})
.then(() => .then(() =>
WorkerNotifications.ready.then(registration => WorkerNotifications.ready.then(registration =>
@ -80,7 +84,10 @@ export const NotificationUserStore = new class {
) )
) )
) )
.catch(e => console.error(e)); .catch(e => {
console.error(e);
WorkerNotifications = null;
});
} else { } else {
const notification = new HTML5Notification(title, options); const notification = new HTML5Notification(title, options);
notification.show?.(); notification.show?.();