From 013019307c31a0c4d2f3763799c8cb4192a17eec Mon Sep 17 00:00:00 2001 From: do0ori Date: Tue, 28 Jan 2025 20:15:50 +0900 Subject: [PATCH] fix: Prevent notification stacking on iOS devices - Added a platform check (`isIOS`) to detect iOS devices. - Disabled notification updates via `setInterval` for iOS to avoid stacking issues. - Ensured consistent timer handling across platforms with platform-specific behavior adjustments. This change addresses the iOS-specific notification behavior where updates do not replace but stack instead, ensuring a smoother user experience on iOS. --- src/service-worker.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/service-worker.ts b/src/service-worker.ts index e341ac0..e123450 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -79,7 +79,8 @@ self.addEventListener('message', (event) => { }); // Any other custom service worker logic can go here. -const timers: Record = {}; +const timers: Record = {}; +const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent); self.addEventListener('message', (event) => { if (event.source) { @@ -105,16 +106,19 @@ self.addEventListener('message', (event) => { }, remainingTime); } - const intervalId = setInterval(async () => { - remainingTime -= 500; + let intervalId; + if (!isIOS) { + intervalId = setInterval(async () => { + remainingTime -= 500; - await self.registration.showNotification(timer.title, { - body: convertMsToMmSs(remainingTime), - icon: '/visual-timer/logo500.png', - tag: timer.id, - silent: true, - }); - }, 500); + await self.registration.showNotification(timer.title, { + body: convertMsToMmSs(remainingTime), + icon: '/visual-timer/logo500.png', + tag: timer.id, + silent: true, + }); + }, 500); + } timers[timer.id] = { timeoutId, intervalId }; } else if (command === 'clear-timer') {