aboutsummaryrefslogtreecommitdiffstats
path: root/src/service-worker.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2024-10-19 21:55:05 -0700
committerAdam Soltys <[email protected]>2024-10-19 21:55:05 -0700
commitde6c1ad727379c5ebe8550124ea9313979124953 (patch)
tree67df287630d6e76484be526522038022a1ccf546 /src/service-worker.js
parent002cd9134d2e23bec34f838f6d6fefba41f2d530 (diff)
downloadcoinos-ui-de6c1ad727379c5ebe8550124ea9313979124953.tar.gz
coinos-ui-de6c1ad727379c5ebe8550124ea9313979124953.zip
push notifications
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);
+ }
+ }),
+ );
});