|
| 1 | +import { |
| 2 | + ExpoSecureStore, |
| 3 | + ExpoApplication, |
| 4 | +} from '../common/optionalDependencies'; |
| 5 | +import type { Logger } from '../common/logger'; |
| 6 | + |
| 7 | +const TOKEN_REGISTERED_KEY = 'sqlite_sync_push_token_registered'; |
| 8 | +const CLOUDSYNC_BASE_URL = 'https://cloudsync-staging.fly.dev/v2'; |
| 9 | + |
| 10 | +async function getDeviceId(): Promise<string> { |
| 11 | + if (!ExpoApplication) { |
| 12 | + throw new Error( |
| 13 | + 'expo-application is required for push notification token registration. Install it with: npx expo install expo-application' |
| 14 | + ); |
| 15 | + } |
| 16 | + |
| 17 | + const { Platform } = require('react-native'); |
| 18 | + if (Platform.OS === 'ios') { |
| 19 | + return await ExpoApplication.getIosIdForVendorAsync(); |
| 20 | + } |
| 21 | + // Android |
| 22 | + return ExpoApplication.getAndroidId(); |
| 23 | +} |
| 24 | + |
| 25 | +interface RegisterPushTokenParams { |
| 26 | + expoToken: string; |
| 27 | + databaseName: string; |
| 28 | + siteId?: string; |
| 29 | + platform: string; |
| 30 | + connectionString: string; |
| 31 | + apiKey?: string; |
| 32 | + accessToken?: string; |
| 33 | + logger: Logger; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Register an Expo push token with the SQLite Cloud backend. |
| 38 | + * Only sends the token once per installation (persisted via SecureStore). |
| 39 | + */ |
| 40 | +export async function registerPushToken( |
| 41 | + params: RegisterPushTokenParams |
| 42 | +): Promise<void> { |
| 43 | + const { |
| 44 | + expoToken, |
| 45 | + databaseName, |
| 46 | + siteId, |
| 47 | + platform, |
| 48 | + connectionString, |
| 49 | + apiKey, |
| 50 | + accessToken, |
| 51 | + logger, |
| 52 | + } = params; |
| 53 | + |
| 54 | + // Check if token was already registered |
| 55 | + if (ExpoSecureStore) { |
| 56 | + try { |
| 57 | + const registered = await ExpoSecureStore.getItemAsync( |
| 58 | + TOKEN_REGISTERED_KEY |
| 59 | + ); |
| 60 | + if (registered === expoToken) { |
| 61 | + logger.info('📱 Push token already registered, skipping'); |
| 62 | + return; |
| 63 | + } |
| 64 | + } catch { |
| 65 | + // Continue with registration |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + const headers: Record<string, string> = { |
| 70 | + 'Content-Type': 'application/json', |
| 71 | + }; |
| 72 | + |
| 73 | + if (accessToken) { |
| 74 | + headers.Authorization = `Bearer ${accessToken}`; |
| 75 | + } else if (apiKey) { |
| 76 | + headers.Authorization = `Bearer ${connectionString}?apikey=${apiKey}`; |
| 77 | + } |
| 78 | + |
| 79 | + const deviceId = await getDeviceId(); |
| 80 | + |
| 81 | + const body = { |
| 82 | + expoToken, |
| 83 | + deviceId, |
| 84 | + database: databaseName, |
| 85 | + siteId: siteId ?? '', |
| 86 | + platform, |
| 87 | + }; |
| 88 | + |
| 89 | + const url = `${CLOUDSYNC_BASE_URL}/cloudsync/notifications/tokens`; |
| 90 | + logger.info( |
| 91 | + '📱 Registering push token with backend...', |
| 92 | + url, |
| 93 | + JSON.stringify(body) |
| 94 | + ); |
| 95 | + |
| 96 | + const response = await fetch(url, { |
| 97 | + method: 'POST', |
| 98 | + headers, |
| 99 | + body: JSON.stringify(body), |
| 100 | + }); |
| 101 | + |
| 102 | + if (!response.ok) { |
| 103 | + const text = await response.text().catch(() => ''); |
| 104 | + throw new Error( |
| 105 | + `Failed to register push token: ${response.status} ${text}` |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + logger.info('📱 Push token registered successfully'); |
| 110 | + |
| 111 | + // Persist that this token has been registered |
| 112 | + if (ExpoSecureStore) { |
| 113 | + try { |
| 114 | + await ExpoSecureStore.setItemAsync(TOKEN_REGISTERED_KEY, expoToken); |
| 115 | + } catch { |
| 116 | + // Non-critical |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments