This commit is contained in:
@@ -42,6 +42,7 @@
|
||||
{
|
||||
<li><a asp-controller="Admin" asp-action="Index"><i class="fas fa-cog"></i> Adminpanel</a></li>
|
||||
}
|
||||
<li><button onclick="enablePush()">🔔 Aktivera notiser</button></li>
|
||||
</ul>
|
||||
</aside>
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
"DefaultConnection": "Server=192.168.1.108;Database=Nevyn;Uid=root;Pwd=3edc4RFV;",
|
||||
"ProductionConnection": "Server=192.168.1.108;Database=Nevyn;Uid=root;Pwd=3edc4RFV;"
|
||||
},
|
||||
"Vapid": {
|
||||
"VapidKeys": {
|
||||
"Subject": "mailto:e@zcz.se",
|
||||
"PublicKey": "BBLmMdU3X3e79SqzAy4vIAJI0jmzRME17F9UKbO8XT1dfnO-mWIPKIrFDbIZD4_3ic7uoijK61vaGdfFUk3HUfU",
|
||||
"PrivateKey": "oranoCmCo8HXdc03juNgbeSlKE39N3DYus_eMunLsnc"
|
||||
|
||||
@@ -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