Skip to content

Commit 0235ace

Browse files
CopilotBruno-366
andcommitted
Fix mobile PWA notifications using service worker
Co-authored-by: Bruno-366 <81762173+Bruno-366@users.noreply.github.com>
1 parent 1ec4aca commit 0235ace

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/App.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ const App = () => {
205205
}
206206
};
207207

208+
// Detect if we're on a mobile device
209+
const isMobileDevice = () => {
210+
return /Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ||
211+
(navigator.maxTouchPoints && navigator.maxTouchPoints > 2);
212+
};
213+
208214
// Request notification permission
209215
const requestNotificationPermission = async () => {
210216
if ('Notification' in window && Notification.permission === 'default') {
@@ -213,9 +219,28 @@ const App = () => {
213219
};
214220

215221
// Show notification when rest timer completes
216-
const showRestCompleteNotification = () => {
222+
const showRestCompleteNotification = async () => {
217223
try {
218224
if ('Notification' in window && Notification.permission === 'granted') {
225+
// For mobile devices, try to use service worker for better reliability
226+
if (isMobileDevice() && 'serviceWorker' in navigator) {
227+
const registration = await navigator.serviceWorker.ready;
228+
229+
// Check if the service worker supports showNotification
230+
if (registration.showNotification) {
231+
await registration.showNotification('Get back to work!', {
232+
body: 'Your rest time is over. Time for the next set!',
233+
icon: '/icon-192x192.png',
234+
badge: '/icon-192x192.png',
235+
tag: 'rest-timer',
236+
requireInteraction: false,
237+
silent: false
238+
});
239+
return;
240+
}
241+
}
242+
243+
// Fallback to regular notification for desktop or when service worker isn't available
219244
new Notification('Get back to work!', {
220245
body: 'Your rest time is over. Time for the next set!',
221246
icon: '/icon-192x192.png',

0 commit comments

Comments
 (0)