From 4c3eeb14b31fbba1a7e81215561023399b46d72d Mon Sep 17 00:00:00 2001 From: Gannicus Date: Sat, 6 Apr 2024 16:12:21 -0400 Subject: [PATCH] Make some updates to the push notification script; not registering, test this out in prod --- src/pages/settings/panes/Notifications.tsx | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/pages/settings/panes/Notifications.tsx b/src/pages/settings/panes/Notifications.tsx index d2dcdee9..cae7a611 100644 --- a/src/pages/settings/panes/Notifications.tsx +++ b/src/pages/settings/panes/Notifications.tsx @@ -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) {