diff options
| author | Kit La Touche <kit@transneptune.net> | 2025-07-30 23:08:40 -0400 |
|---|---|---|
| committer | Kit La Touche <kit@transneptune.net> | 2025-07-30 23:08:40 -0400 |
| commit | ed5e175a806f45469a6e5504ba0d3f5246997fad (patch) | |
| tree | 0d4233c57596186b86d165640ca4721e7495567d /ui/routes | |
| parent | b63380b251d04dd92f06aa5bbc22a72ca3e4bf8e (diff) | |
Test receiving push events when backgrounded
And thus also displaying notifications.
Diffstat (limited to 'ui/routes')
| -rw-r--r-- | ui/routes/+layout.svelte | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/ui/routes/+layout.svelte b/ui/routes/+layout.svelte index 9089c7e..841d597 100644 --- a/ui/routes/+layout.svelte +++ b/ui/routes/+layout.svelte @@ -10,7 +10,7 @@ function doSubscribe() { navigator.serviceWorker.ready - .then(async(registration) => { + .then(async (registration) => { const response = await fetch("/api/vapid"); // and if we fail to get it? const vapidPublicKey = await response.text(); @@ -20,8 +20,13 @@ applicationServerKey: convertedVapidKey }); }).then((subscription) => { - subscriptionJson = JSON.parse(JSON.stringify(subscription)); - return fetch("/api/register", { + const subJson = subscription.toJSON(); + subscriptionJson = { + endpoint: subJson.endpoint, + p256dh: subJson.keys.p256dh, + auth: subJson.keys.auth, + }; + return fetch("/api/push", { method: "post", headers: { "Content-type": "application/json" }, body: JSON.stringify(subscriptionJson), @@ -34,14 +39,15 @@ .then((registration) => { return registration.pushManager.getSubscription(); }).then((subscription) => { + const { endpoint } = subscription.toJSON(); return subscription.unsubscribe() - .then(function() { - subscriptionJson = null; - return fetch("/api/unregister", { - method: "post", + .then(() => { + fetch("/api/push", { + method: "delete", headers: { "Content-type": "application/json" }, - body: JSON.stringify({ subscription }) + body: JSON.stringify({ endpoint }), }); + subscriptionJson = null; }); }); } @@ -59,8 +65,8 @@ method: "post", headers: { "Content-type": "application/json" }, body: JSON.stringify({ - ...subscriptionJson, - msg: "oople doople", + endpoint: subscriptionJson.endpoint, + msg: JSON.stringify({"msg": "oople doople"}), }) }); } |
