Make some updates to the push notification script; not registering, test this out in prod

pull/1140/head
Gannicus 2024-04-06 16:12:21 -04:00
parent 43e9ec8a8f
commit 4c3eeb14b3
1 changed files with 21 additions and 2 deletions

View File

@ -26,6 +26,7 @@ export const Notifications = observer(() => {
?.getRegistration()
.then(async (registration) => {
const sub = await registration?.pushManager?.getSubscription();
console.log(sub);
setPushEnabled(sub !== null && sub !== undefined);
});
}, []);
@ -73,8 +74,24 @@ export const Notifications = observer(() => {
}
onChange={async (pushEnabled) => {
try {
const reg =
await navigator.serviceWorker?.getRegistration();
if (!("serviceWorker" in navigator)) {
return modalController.push({
type: "error",
error: "NoServiceWorker",
});
}
let reg =
await navigator.serviceWorker.getRegistration(
`${window.location.origin}/sw.js`,
);
if (!reg) {
reg =
await navigator.serviceWorker.register(
`${window.location.origin}/sw.js`,
);
}
if (reg) {
if (pushEnabled) {
const sub =
@ -87,6 +104,8 @@ export const Notifications = observer(() => {
),
});
console.log(sub);
// tell the server we just subscribed
const json = sub.toJSON();
if (json.keys) {