From 0ef20bd8d9ab0f12ace75cdbbd0e5767a5f40d3a Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 25 Jul 2024 04:10:39 -0700 Subject: fix(service-worker): show default notification for chrome --- src/service-worker.ts | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/service-worker.ts b/src/service-worker.ts index 21de63a2..d7cc2cdd 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -125,17 +125,25 @@ sw.addEventListener('push', async (event: PushEvent) => { console.error(error); } - // 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); - }); + if (navigator.userAgent.includes('Chrome')) { + sw.registration.showNotification('due.moe', { + body: 'No new notifications', + icon: '/favicon-196x196.png', + tag: 'notification-1' + }); + + // Ref. https://github.com/firebase/quickstart-js/issues/126#issuecomment-504081087 + new Promise((resolve) => { + (resolve as unknown as () => void)(); + setTimeout( + () => + sw.registration + .getNotifications() + .then((notifications) => notifications.forEach((notification) => notification.close())), + 10 + ); + }); + } }); sw.addEventListener('notificationclick', (event: NotificationEvent) => { -- cgit v1.2.3