File tree Expand file tree Collapse file tree 3 files changed +49
-35
lines changed
src/core/pushNotifications Expand file tree Collapse file tree 3 files changed +49
-35
lines changed Original file line number Diff line number Diff line change 1+ const ARTIFACT_URI = 'https://sqlite.ai' ;
2+
3+ /**
4+ * Check if a foreground Expo Notification is from SQLite Cloud.
5+ * Foreground notifications use the Expo Notification object structure:
6+ * `notification.request.content.data.artifactURI`
7+ */
8+ export const isForegroundSqliteCloudNotification = (
9+ notification : any
10+ ) : boolean => {
11+ const artifactURI = notification ?. request ?. content ?. data ?. artifactURI ;
12+ return artifactURI === ARTIFACT_URI ;
13+ } ;
14+
15+ /**
16+ * Check if background/terminated task data is from SQLite Cloud.
17+ * Handles platform differences:
18+ * - iOS: `data.body` is an object with `artifactURI`
19+ * - Android: `data.body` or `data.dataString` is a JSON string containing `artifactURI`
20+ * Also checks the foreground structure as a fallback.
21+ */
22+ export const isSqliteCloudNotification = ( notification : any ) : boolean => {
23+ const body = notification ?. data ?. body ;
24+
25+ // iOS background
26+ if ( body && typeof body === 'object' && body . artifactURI === ARTIFACT_URI ) {
27+ return true ;
28+ }
29+
30+ // Android background
31+ const bodyString =
32+ typeof body === 'string' ? body : notification ?. data ?. dataString ;
33+ if ( typeof bodyString === 'string' ) {
34+ try {
35+ const parsed = JSON . parse ( bodyString ) ;
36+ if ( parsed ?. artifactURI === ARTIFACT_URI ) {
37+ return true ;
38+ }
39+ } catch {
40+ // Not valid JSON
41+ }
42+ }
43+
44+ // Foreground notification structure (fallback)
45+ return isForegroundSqliteCloudNotification ( notification ) ;
46+ } ;
Original file line number Diff line number Diff line change @@ -5,33 +5,7 @@ import { createLogger } from '../common/logger';
55import { BACKGROUND_SYNC_TASK_NAME } from '../constants' ;
66import { executeBackgroundSync } from '../background/executeBackgroundSync' ;
77import { getForegroundSyncCallback } from './pushNotificationSyncCallbacks' ;
8-
9- /**
10- * Check if task data is from SQLite Cloud.
11- * Handles different data structures from foreground vs background notifications.
12- */
13- const isSqliteCloudNotification = ( taskData : any ) : boolean => {
14- // Background notification: data is in taskData.data.body (as JSON string)
15- const bodyString = taskData ?. data ?. body || taskData ?. data ?. dataString ;
16- if ( bodyString ) {
17- try {
18- const parsed = JSON . parse ( bodyString ) ;
19- if ( parsed ?. artifactURI === 'https://sqlite.ai' ) {
20- return true ;
21- }
22- } catch {
23- // Not valid JSON
24- }
25- }
26-
27- // Foreground notification structure
28- const artifactURI = taskData ?. request ?. content ?. data ?. artifactURI ;
29- if ( artifactURI === 'https://sqlite.ai' ) {
30- return true ;
31- }
32-
33- return false ;
34- } ;
8+ import { isSqliteCloudNotification } from './isSqliteCloudNotification' ;
359
3610/**
3711 * Auto-define background task at module level.
Original file line number Diff line number Diff line change @@ -97,13 +97,7 @@ export interface PushNotificationSyncParams {
9797 onBeforePushPermissionRequest ?: ( ) => Promise < boolean > ;
9898}
9999
100- /**
101- * Check if a notification is from SQLite Cloud
102- */
103- const isSqliteCloudNotification = ( notification : any ) : boolean => {
104- const artifactURI = notification ?. request ?. content ?. data ?. artifactURI ;
105- return artifactURI === 'https://sqlite.ai' ;
106- } ;
100+ import { isForegroundSqliteCloudNotification } from './isSqliteCloudNotification' ;
107101
108102export function usePushNotificationSync (
109103 params : PushNotificationSyncParams
@@ -279,7 +273,7 @@ export function usePushNotificationSync(
279273 JSON . stringify ( notification , null , 2 )
280274 ) ;
281275
282- if ( isSqliteCloudNotification ( notification ) ) {
276+ if ( isForegroundSqliteCloudNotification ( notification ) ) {
283277 logger . info (
284278 '📲 SQLite Cloud notification (foreground) - triggering sync'
285279 ) ;
You can’t perform that action at this time.
0 commit comments