diff options
| author | Fuwn <[email protected]> | 2024-07-25 02:55:38 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-07-25 02:55:38 -0700 |
| commit | aa7870b2eae80a6dbdc1a92dc2429b1e66d3c1f7 (patch) | |
| tree | 54760bbd4f1e075b8cef7f6241667d80b73a3670 | |
| parent | chore(service-worker): fix types (diff) | |
| download | due.moe-aa7870b2eae80a6dbdc1a92dc2429b1e66d3c1f7.tar.xz due.moe-aa7870b2eae80a6dbdc1a92dc2429b1e66d3c1f7.zip | |
fix(pwa): robust silent notifications
| -rw-r--r-- | src/service-worker.ts | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/service-worker.ts b/src/service-worker.ts index d7629073..21de63a2 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -114,7 +114,9 @@ sw.addEventListener('push', async (event: PushEvent) => { sw.registration.showNotification('due.moe', { body: `${recentNotifications[0].user.name}${recentNotifications[0].context}`, - icon: recentNotifications[0].user.avatar.large || '/favicon-196x196.png' + icon: recentNotifications[0].user.avatar.large || '/favicon-196x196.png', + // Ref. https://stackoverflow.com/a/50805868/14452787 + tag: 'notification-1' }); return; @@ -123,11 +125,17 @@ sw.addEventListener('push', async (event: PushEvent) => { console.error(error); } - event.waitUntil( - new Promise(() => { - // - }) - ); + // Ref. https://github.com/firebase/quickstart-js/issues/126#issuecomment-504081087 + return new Promise((resolve) => { + (resolve as unknown as () => void)(); + setTimeout(() => { + sw.registration.getNotifications().then((notifications) => { + notifications.forEach((notification) => { + if (notification.tag !== 'notification') notification.close(); + }); + }); + }, 10); + }); }); sw.addEventListener('notificationclick', (event: NotificationEvent) => { |