Update service worker.
This commit is contained in:
parent
45e3239cc0
commit
c3c94461c2
@ -1,3 +1,37 @@
|
|||||||
|
async function requestNotificationPermission() {
|
||||||
|
const permission = await Notification.requestPermission();
|
||||||
|
return permission === 'granted';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscribe to Push Notifications
|
||||||
|
async function subscribeUser() {
|
||||||
|
const registration = await navigator.serviceWorker.register('/service-worker.js');
|
||||||
|
|
||||||
|
const subscription = await registration.pushManager.subscribe({
|
||||||
|
userVisibleOnly: true,
|
||||||
|
applicationServerKey: urlBase64ToUint8Array(PUBLIC_VAPID_KEY)
|
||||||
|
});
|
||||||
|
|
||||||
|
// Send subscription to your backend
|
||||||
|
await fetch('/subscribe', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(subscription),
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Service Worker (service-worker.js)
|
||||||
|
self.addEventListener('push', event => {
|
||||||
|
const data = event.data.json();
|
||||||
|
self.registration.showNotification(data.title, {
|
||||||
|
body: data.message,
|
||||||
|
icon: data.icon
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
self.addEventListener("install", (event) => {
|
self.addEventListener("install", (event) => {
|
||||||
console.log("Service worker installed");
|
console.log("Service worker installed");
|
||||||
});
|
});
|
||||||
@ -27,4 +61,4 @@ self.addEventListener("notificationclick", (event) => {
|
|||||||
event.notification.close();
|
event.notification.close();
|
||||||
event.waitUntil(clients.openWindow(
|
event.waitUntil(clients.openWindow(
|
||||||
"https://snek.molodetz.nl",));
|
"https://snek.molodetz.nl",));
|
||||||
});
|
});*/
|
||||||
|
Loading…
Reference in New Issue
Block a user