From df829778daddcf2ba338362b98a93a1beb5e0374 Mon Sep 17 00:00:00 2001 From: Trent Larson Date: Wed, 10 Apr 2024 19:31:14 -0600 Subject: [PATCH] open the app when notification is clicked --- sw_scripts/additional-scripts.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sw_scripts/additional-scripts.js b/sw_scripts/additional-scripts.js index 1067606f..07e5d2d2 100644 --- a/sw_scripts/additional-scripts.js +++ b/sw_scripts/additional-scripts.js @@ -112,6 +112,25 @@ self.addEventListener("message", (event) => { logConsoleAndDb("Service worker posted a message."); }); +self.addEventListener("notificationclick", (event) => { + logConsoleAndDb("Notification got clicked.", event); + event.notification.close(); + // from https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/notificationclick_event + // ... though I don't see any benefit over just "clients.openWindow" + event.waitUntil( + clients + .matchAll({ + type: "window", + }) + .then((clientList) => { + for (const client of clientList) { + if (client.url === "/" && "focus" in client) return client.focus(); + } + if (clients.openWindow) return clients.openWindow("/"); + }), + ); +}); + self.addEventListener("fetch", (event) => { logConsoleAndDb("Service worker got fetch event.", event); });