Skip to content

Commit 610a164

Browse files
committed
fix: registerPushToken - register the token at every app open
1 parent 3234c2c commit 610a164

File tree

2 files changed

+6
-76
lines changed

2 files changed

+6
-76
lines changed

src/core/pushNotifications/__tests__/registerPushToken.test.ts

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
import { registerPushToken } from '../registerPushToken';
22
import { createLogger } from '../../common/logger';
3-
import {
4-
ExpoSecureStore,
5-
ExpoApplication,
6-
} from '../../common/optionalDependencies';
3+
import { ExpoApplication } from '../../common/optionalDependencies';
74

85
jest.mock('../../common/optionalDependencies', () => ({
9-
ExpoSecureStore: {
10-
getItemAsync: jest.fn().mockResolvedValue(null),
11-
setItemAsync: jest.fn().mockResolvedValue(undefined),
12-
},
136
ExpoApplication: {
147
getIosIdForVendorAsync: jest.fn().mockResolvedValue('mock-ios-vendor-id'),
158
getAndroidId: jest.fn().mockReturnValue('mock-android-id'),
@@ -49,16 +42,6 @@ describe('registerPushToken', () => {
4942
Platform.OS = 'ios';
5043
});
5144

52-
it('skips registration if token is already registered', async () => {
53-
(ExpoSecureStore!.getItemAsync as jest.Mock).mockResolvedValueOnce(
54-
'ExponentPushToken[abc123]'
55-
);
56-
57-
await registerPushToken(baseParams);
58-
59-
expect(mockFetch).not.toHaveBeenCalled();
60-
});
61-
6245
it('sends request to the correct URL', async () => {
6346
await registerPushToken(baseParams);
6447

@@ -135,30 +118,11 @@ describe('registerPushToken', () => {
135118
);
136119
});
137120

138-
it('persists token after successful registration', async () => {
121+
it('registers token on every call', async () => {
122+
await registerPushToken(baseParams);
139123
await registerPushToken(baseParams);
140124

141-
expect(ExpoSecureStore!.setItemAsync).toHaveBeenCalledWith(
142-
'sqlite_sync_push_token_registered',
143-
'ExponentPushToken[abc123]'
144-
);
145-
});
146-
147-
it('handles SecureStore read errors gracefully', async () => {
148-
(ExpoSecureStore!.getItemAsync as jest.Mock).mockRejectedValueOnce(
149-
new Error('SecureStore read failed')
150-
);
151-
152-
await expect(registerPushToken(baseParams)).resolves.toBeUndefined();
153-
expect(mockFetch).toHaveBeenCalled();
154-
});
155-
156-
it('handles SecureStore write errors gracefully', async () => {
157-
(ExpoSecureStore!.setItemAsync as jest.Mock).mockRejectedValueOnce(
158-
new Error('SecureStore write failed')
159-
);
160-
161-
await expect(registerPushToken(baseParams)).resolves.toBeUndefined();
125+
expect(mockFetch).toHaveBeenCalledTimes(2);
162126
});
163127

164128
it('throws when ExpoApplication is null', async () => {

src/core/pushNotifications/registerPushToken.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import {
2-
ExpoSecureStore,
3-
ExpoApplication,
4-
} from '../common/optionalDependencies';
1+
import { ExpoApplication } from '../common/optionalDependencies';
52
import type { Logger } from '../common/logger';
63
import { CLOUDSYNC_BASE_URL } from '../constants';
74

8-
const TOKEN_REGISTERED_KEY = 'sqlite_sync_push_token_registered';
9-
105
async function getDeviceId(): Promise<string> {
116
if (!ExpoApplication) {
127
throw new Error(
@@ -34,7 +29,7 @@ interface RegisterPushTokenParams {
3429

3530
/**
3631
* Register an Expo push token with the SQLite Cloud backend.
37-
* Only sends the token once per installation (persisted via SecureStore).
32+
* Sends the token on every call to ensure the backend always has a valid token.
3833
*/
3934
export async function registerPushToken(
4035
params: RegisterPushTokenParams
@@ -49,21 +44,6 @@ export async function registerPushToken(
4944
logger,
5045
} = params;
5146

52-
/** CHECK IF ALREADY REGISTERED */
53-
if (ExpoSecureStore) {
54-
try {
55-
const registered = await ExpoSecureStore.getItemAsync(
56-
TOKEN_REGISTERED_KEY
57-
);
58-
if (registered === expoToken) {
59-
logger.info('📱 Push token already registered, skipping');
60-
return;
61-
}
62-
} catch {
63-
// Continue with registration
64-
}
65-
}
66-
6747
/** PREPARE REQUEST HEADERS */
6848
const headers: Record<string, string> = {
6949
'Content-Type': 'application/json',
@@ -93,11 +73,6 @@ export async function registerPushToken(
9373
const url = `${CLOUDSYNC_BASE_URL}/v2/cloudsync/databases/${encodeURIComponent(
9474
databaseId
9575
)}/notifications/tokens`;
96-
logger.info(
97-
'📱 Registering push token with backend...',
98-
url,
99-
JSON.stringify(body)
100-
);
10176

10277
const response = await fetch(url, {
10378
method: 'PUT',
@@ -113,13 +88,4 @@ export async function registerPushToken(
11388
}
11489

11590
logger.info('📱 Push token registered successfully');
116-
117-
/** PERSIST REGISTRATION STATUS */
118-
if (ExpoSecureStore) {
119-
try {
120-
await ExpoSecureStore.setItemAsync(TOKEN_REGISTERED_KEY, expoToken);
121-
} catch {
122-
// Non-critical
123-
}
124-
}
12591
}

0 commit comments

Comments
 (0)