From 43ab6533daa46b9b47a11cf351ef770fe5ce3f68 Mon Sep 17 00:00:00 2001 From: Patrick Brosset Date: Mon, 4 Nov 2024 16:28:42 +0100 Subject: [PATCH] Add comms with SW --- incoming-call-notifications/sw.js | 71 ++----------------------------- 1 file changed, 4 insertions(+), 67 deletions(-) diff --git a/incoming-call-notifications/sw.js b/incoming-call-notifications/sw.js index 316ac08..fdf56ea 100644 --- a/incoming-call-notifications/sw.js +++ b/incoming-call-notifications/sw.js @@ -6,81 +6,18 @@ onactivate = () => { return clients.claim(); }; -onmessage = (messageEvent) => { - registration - .showNotification(`ServiceWorkerGlobalScope Title ${messageEvent.data}`, { - body: `ServiceWorkerGlobalScope Body ${messageEvent.data}`, - icon: "../resources/happy.jpg", - }) - .then(() => { - messageEvent.source.postMessage( - "ServiceWorkerGlobalScope showNotification() succeeded." - ); - }) - .catch((error) => { - messageEvent.source.postMessage( - `ServiceWorkerGlobalScope showNotification() failed: ${error}.` - ); - }); +onfetch = (fetchEvent) => { + console.log(fetchEvent.request.url); + fetchEvent.respondWith(fetch(fetchEvent.request)); }; onnotificationclick = (event) => { - const notification = event.notification; - - clients.matchAll().then((resultList) => { - resultList.forEach((client) => { - client.postMessage( - `ServiceWorkerGlobalScope 'click' event for: ${ - notification.title - } , timestamp: ${new Date( - notification.timestamp - )}, requireInteraction: ${notification.requireInteraction}, silent: ${ - notification.silent - }` - ); - notification.close(); - }); - }); - - if (event.action === "open_window") { - event.waitUntil( - new Promise((resolve) => { - setTimeout(() => { - clients.openWindow("on-click.html"); - resolve(); - }, 0); - }) - ); - } else { - // Focus existing client. - event.waitUntil( - clients.matchAll().then((resultList) => { - if (resultList.length > 0) { - return resultList[0].focus(); - } - }) - ); - } -}; - -onnotificationclose = (event) => { clients.matchAll().then((resultList) => { resultList.forEach((client) => { const notification = event.notification; client.postMessage( - `ServiceWorkerGlobalScope 'close' event for: ${ - notification.title - } , timestamp: ${new Date( - notification.timestamp - )}, requireInteraction: ${notification.requireInteraction}, silent: ${ - notification.silent - }` + `Service-worker: Action '${event.action}' for: ${notification.title}` ); }); }); }; - -onfetch = (fetchEvent) => { - console.log(fetchEvent.request.url); - fetchEvent.respondWith(fetch(fetchEvent.request)); -};