App and push notifications?
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
BIN
Aberwyn/wwwroot/images/lewel-icon.png
Normal file
BIN
Aberwyn/wwwroot/images/lewel-icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.0 MiB |
@@ -1,4 +1,42 @@
|
||||
// Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
|
||||
// for details on configuring this project to bundle and minify static web assets.
|
||||
|
||||
// Write your JavaScript code.
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', function () {
|
||||
navigator.serviceWorker.register('/service-worker.js')
|
||||
.then(function (registration) {
|
||||
console.log('✅ Service Worker registrerad med scope:', registration.scope);
|
||||
subscribeToPush().catch(console.error);
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.log('❌ Service Worker-registrering misslyckades:', error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function subscribeToPush() {
|
||||
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)
|
||||
});
|
||||
|
||||
await fetch('/api/push/subscribe', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(subscription),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
|
||||
console.log('✅ Push-prenumeration skickad');
|
||||
}
|
||||
|
||||
|
||||
// utility för att konvertera nyckeln
|
||||
function urlBase64ToUint8Array(base64String) {
|
||||
const padding = '='.repeat((4 - base64String.length % 4) % 4);
|
||||
const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');
|
||||
const raw = atob(base64);
|
||||
return new Uint8Array([...raw].map(char => char.charCodeAt(0)));
|
||||
}
|
||||
|
||||
15
Aberwyn/wwwroot/manifest.json
Normal file
15
Aberwyn/wwwroot/manifest.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "LEWEL",
|
||||
"short_name": "LEWEL",
|
||||
"start_url": "/",
|
||||
"display": "standalone",
|
||||
"background_color": "#1F2C3C",
|
||||
"theme_color": "#6a0dad",
|
||||
"icons": [
|
||||
{
|
||||
"src": "/images/lewel-icon.png",
|
||||
"sizes": "512x512",
|
||||
"type": "image/png"
|
||||
}
|
||||
]
|
||||
}
|
||||
36
Aberwyn/wwwroot/service-worker.js
Normal file
36
Aberwyn/wwwroot/service-worker.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const CACHE_NAME = 'lewel-cache-v1';
|
||||
const urlsToCache = [
|
||||
'/',
|
||||
'/css/site.css',
|
||||
'/js/site.js',
|
||||
'/icons/lewel-icon.png',
|
||||
'/manifest.json'
|
||||
];
|
||||
|
||||
self.addEventListener('install', event => {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME).then(cache => {
|
||||
return cache.addAll(urlsToCache);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', event => {
|
||||
event.respondWith(
|
||||
caches.match(event.request)
|
||||
.then(response => response || fetch(event.request))
|
||||
);
|
||||
});
|
||||
self.addEventListener('push', function (event) {
|
||||
const data = event.data ? event.data.json() : { title: 'LEWEL', body: 'Ny notis!' };
|
||||
|
||||
const options = {
|
||||
body: data.body,
|
||||
icon: '/icons/lewel-icon.png',
|
||||
badge: '/icons/lewel-icon.png'
|
||||
};
|
||||
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(data.title, options)
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user