aboutsummaryrefslogtreecommitdiffstats
path: root/src/service-worker.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/service-worker.js')
-rw-r--r--src/service-worker.js32
1 files changed, 30 insertions, 2 deletions
diff --git a/src/service-worker.js b/src/service-worker.js
index fee7eee0..fa934333 100644
--- a/src/service-worker.js
+++ b/src/service-worker.js
@@ -4,6 +4,34 @@ self.addEventListener("install", (event) => {});
self.addEventListener("activate", (event) => {});
self.addEventListener("fetch", (event) => {});
self.addEventListener("push", (event) => {
- const pushData = event.data.json();
- self.registration.showNotification(pushData.title, pushData);
+ const { title, body, url } = event.data.json();
+ const icon = "/images/icon.png";
+ const badge = "/images/badge.png";
+ self.registration.showNotification(title, {
+ body,
+ icon,
+ badge,
+ data: { url },
+ });
+});
+
+self.addEventListener("notificationclick", (event) => {
+ event.notification.close();
+
+ const url = event.notification.data.url || "/";
+
+ event.waitUntil(
+ clients
+ .matchAll({ type: "window", includeUncontrolled: true })
+ .then((clientList) => {
+ for (const client of clientList) {
+ if (client.url === url && "focus" in client) {
+ return client.focus();
+ }
+ }
+ if (clients.openWindow) {
+ return clients.openWindow(url);
+ }
+ }),
+ );
});