This commit is contained in:
@@ -32,6 +32,34 @@ async function subscribeToPush() {
|
||||
console.log('✅ Push-prenumeration skickad');
|
||||
}
|
||||
|
||||
async function enablePush() {
|
||||
const permission = await Notification.requestPermission();
|
||||
|
||||
if (permission !== "granted") {
|
||||
alert("Du måste tillåta notiser för att få push.");
|
||||
return;
|
||||
}
|
||||
|
||||
const registration = await navigator.serviceWorker.ready;
|
||||
const publicVapidKey = await fetch('/api/push/vapid-public-key').then(r => r.text());
|
||||
|
||||
const subscription = await registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: urlBase64ToUint8Array(publicVapidKey.trim()) // 👈 .trim() viktigt
|
||||
});
|
||||
|
||||
|
||||
await fetch("/api/push/subscribe", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(subscription),
|
||||
headers: { "Content-Type": "application/json" }
|
||||
});
|
||||
|
||||
alert("✅ Push-notiser aktiverade!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// utility för att konvertera nyckeln
|
||||
function urlBase64ToUint8Array(base64String) {
|
||||
|
||||
@@ -3,7 +3,7 @@ const urlsToCache = [
|
||||
'/',
|
||||
'/css/site.css',
|
||||
'/js/site.js',
|
||||
'/icons/lewel-icon.png',
|
||||
'/images/lewel-icon.png',
|
||||
'/manifest.json'
|
||||
];
|
||||
|
||||
@@ -22,7 +22,9 @@ self.addEventListener('fetch', event => {
|
||||
);
|
||||
});
|
||||
self.addEventListener('push', function (event) {
|
||||
const data = event.data ? event.data.json() : { title: 'LEWEL', body: 'Ny notis!' };
|
||||
console.log("📨 Push event mottagen!", event);
|
||||
|
||||
const data = event.data ? event.data.json() : { title: "LEWEL", body: "Ny notis" };
|
||||
|
||||
const options = {
|
||||
body: data.body,
|
||||
@@ -34,3 +36,5 @@ self.addEventListener('push', function (event) {
|
||||
self.registration.showNotification(data.title, options)
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user