From ed5e175a806f45469a6e5504ba0d3f5246997fad Mon Sep 17 00:00:00 2001 From: Kit La Touche Date: Wed, 30 Jul 2025 23:08:40 -0400 Subject: Test receiving push events when backgrounded And thus also displaying notifications. --- ui/routes/+layout.svelte | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'ui/routes/+layout.svelte') 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"}), }) }); } -- cgit v1.2.3