-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
26 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters