Skip to content

Commit 8af3fd0

Browse files
committed
refactor: addForegroundListener
1 parent 7a76881 commit 8af3fd0

File tree

1 file changed

+17
-32
lines changed

1 file changed

+17
-32
lines changed

src/core/pushNotifications/usePushNotificationSync.ts

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,21 @@ export function usePushNotificationSync(
272272
`📲 SQLite Sync push mode enabled (listening: ${notificationListening})`
273273
);
274274

275+
const addForegroundListener = () =>
276+
ExpoNotifications.addNotificationReceivedListener((notification: any) => {
277+
logger.info(
278+
'📬 Foreground notification received:',
279+
JSON.stringify(notification, null, 2)
280+
);
281+
282+
if (isSqliteCloudNotification(notification)) {
283+
logger.info(
284+
'📲 SQLite Cloud notification (foreground) - triggering sync'
285+
);
286+
performSyncRef.current?.();
287+
}
288+
});
289+
275290
const subscriptions: { remove: () => void }[] = [];
276291

277292
// BACKGROUND & TERMINATED: Register background task
@@ -296,41 +311,11 @@ export function usePushNotificationSync(
296311
'⚠️ Background sync not available. Install expo-task-manager and expo-secure-store for background/terminated notification handling.'
297312
);
298313
// Fallback to foreground-only listener
299-
const foregroundSubscription =
300-
ExpoNotifications.addNotificationReceivedListener(
301-
(notification: any) => {
302-
logger.info(
303-
'📬 RAW notification received:',
304-
JSON.stringify(notification.request?.content)
305-
);
306-
if (isSqliteCloudNotification(notification)) {
307-
logger.info(
308-
'📲 SQLite Cloud notification (foreground) - triggering sync'
309-
);
310-
performSyncRef.current?.();
311-
}
312-
}
313-
);
314-
subscriptions.push(foregroundSubscription);
314+
subscriptions.push(addForegroundListener());
315315
}
316316
} else {
317317
// FOREGROUND ONLY: Use traditional listener
318-
const foregroundSubscription =
319-
ExpoNotifications.addNotificationReceivedListener(
320-
(notification: any) => {
321-
logger.info(
322-
'📬 RAW notification received:',
323-
JSON.stringify(notification.request?.content)
324-
);
325-
if (isSqliteCloudNotification(notification)) {
326-
logger.info(
327-
'📲 SQLite Cloud notification (foreground) - triggering sync'
328-
);
329-
performSyncRef.current?.();
330-
}
331-
}
332-
);
333-
subscriptions.push(foregroundSubscription);
318+
subscriptions.push(addForegroundListener());
334319
}
335320

336321
return () => {

0 commit comments

Comments
 (0)