diff --git a/packages/im/src/ImSDK.ts b/packages/im/src/ImSDK.ts index 296fbd4..219302b 100644 --- a/packages/im/src/ImSDK.ts +++ b/packages/im/src/ImSDK.ts @@ -312,6 +312,17 @@ export const ImSDK = { client.addListener({ onConnected: () => { _syncHistoryForAllConversations().catch(() => {}) + // Auto-register push token if Push module is installed and a token is pending + import('@xuqm/rn-push') + .then(({ PushSDK }) => { + const pending = PushSDK.getPendingToken?.() + if (pending) { + PushSDK.registerToken(userId, pending.token, pending.vendor).catch(() => {}) + } + }) + .catch(() => { + // Push module not installed — ignore gracefully + }) }, }) void client.connect() @@ -1096,6 +1107,12 @@ export const ImSDK = { return normalized }, + /** + * Subscribe to conversation list changes. + * Returns the current conversation list immediately via the callback, + * then invokes the callback again whenever the list changes. + * @returns An unsubscribe function. + */ subscribeConversations(callback: (conversations: ConversationData[]) => void): () => void { if (!ImDatabase.isInitialized()) { conversationListeners.add(callback) diff --git a/packages/push/src/PushSDK.ts b/packages/push/src/PushSDK.ts index 4513cf1..55520b4 100644 --- a/packages/push/src/PushSDK.ts +++ b/packages/push/src/PushSDK.ts @@ -40,6 +40,21 @@ export const PushSDK = { await registerPendingToken() }, + /** + * Cache a device token without immediately registering it. + * Call this when the native layer receives a token before the user is logged in. + */ + setPendingToken(token: string, vendor?: PushVendor): void { + pendingToken = { token, vendor } + }, + + /** + * Get the currently cached pending token, if any. + */ + getPendingToken(): PendingDeviceToken | null { + return pendingToken + }, + async setDeviceToken(token: string, vendor?: PushVendor): Promise { pendingToken = { token, vendor } await registerPendingToken() @@ -102,6 +117,7 @@ export const PushSDK = { osVersion: device.osVersion, }, }) + pendingToken = null }, async unregisterToken(userId: string): Promise {