Browse Source

open the app when notification is clicked

pull/114/head
Trent Larson 7 months ago
parent
commit
df829778da
  1. 19
      sw_scripts/additional-scripts.js

19
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);
});

Loading…
Cancel
Save