Skip to content

Commit

Permalink
fix: Prevent notification stacking on iOS devices
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
do0ori committed Jan 28, 2025
1 parent 24db243 commit 0130193
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ self.addEventListener('message', (event) => {
});

// Any other custom service worker logic can go here.
const timers: Record<string, { timeoutId: NodeJS.Timeout | undefined; intervalId: NodeJS.Timer }> = {};
const timers: Record<string, { timeoutId: NodeJS.Timeout | undefined; intervalId: NodeJS.Timer | undefined }> = {};
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);

self.addEventListener('message', (event) => {
if (event.source) {
Expand All @@ -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') {
Expand Down

0 comments on commit 0130193

Please # to comment.