Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jellybob committed Apr 15, 2024
1 parent 1b6ccc2 commit 531276e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
48 changes: 25 additions & 23 deletions js/notifications.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
async function enableNotifications(event) {
let vapid_key = document.querySelector("meta[name=vapid_key]").getAttribute("value");
let worker = await navigator.serviceWorker.ready;
let result = await worker.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: vapid_key,
});
let vapid_key = document.querySelector("meta[name=vapid_key]").getAttribute("value");
let worker = await navigator.serviceWorker.ready;
let result = await worker.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: vapid_key,
});

let response = await fetch("/account/notifications/register", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(result.toJSON())
});
let response = await fetch("/account/notifications/register", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(result.toJSON())
});
}

async function checkPermissions() {
let worker = await navigator.serviceWorker.ready;
let worker = await navigator.serviceWorker.ready;

if ("pushManager" in worker) {
let permissions = await worker.pushManager.permissionState();
document.getElementById('notification-state').querySelectorAll('.state').forEach(el => { el.classList.add('visible') })
document.getElementById(`notification-state-${permissions}`).classList.add('visible')
} else {
console.log("No push notification support.");
}
if ("pushManager" in worker) {
let permissions = await worker.pushManager.permissionState({
userVisibleOnly: true,
});
document.getElementById(`notification-state-${permissions}`).classList.add('visible')
} else {
document.getElementById(`notification-state-unsupported`).classList.add('visible');
console.log("No push notification support.");
}
}

if ("serviceWorker" in navigator) {
checkPermissions();
document.getElementById("enable-notifications").addEventListener("click", enableNotifications);
checkPermissions();
document.getElementById("enable-notifications").addEventListener("click", enableNotifications);
}
2 changes: 1 addition & 1 deletion js/serviceworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ registerRoute(

addEventListener("push", (event) => {
console.log("Push event received", event);
const message = event.data.text()
const message = event.data.text();
self.registration.showNotification(message);
});

Expand Down

0 comments on commit 531276e

Please sign in to comment.