diff options
| author | Adam Soltys <[email protected]> | 2024-10-19 21:55:05 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-10-19 21:55:05 -0700 |
| commit | de6c1ad727379c5ebe8550124ea9313979124953 (patch) | |
| tree | 67df287630d6e76484be526522038022a1ccf546 /src/service-worker.js | |
| parent | 002cd9134d2e23bec34f838f6d6fefba41f2d530 (diff) | |
| download | coinos-ui-de6c1ad727379c5ebe8550124ea9313979124953.tar.gz coinos-ui-de6c1ad727379c5ebe8550124ea9313979124953.zip | |
push notifications
Diffstat (limited to 'src/service-worker.js')
| -rw-r--r-- | src/service-worker.js | 32 |
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); + } + }), + ); }); |
