diff --git a/ios/OutLive/Info.plist b/ios/OutLive/Info.plist index 93b69e0..d6d8385 100644 --- a/ios/OutLive/Info.plist +++ b/ios/OutLive/Info.plist @@ -26,7 +26,7 @@ CFBundlePackageType $(PRODUCT_BUNDLE_PACKAGE_TYPE) CFBundleShortVersionString - 1.0.22 + 1.0.23 CFBundleSignature ???? CFBundleURLTypes diff --git a/services/pushNotificationManager.ts b/services/pushNotificationManager.ts index b3a749b..300220a 100644 --- a/services/pushNotificationManager.ts +++ b/services/pushNotificationManager.ts @@ -161,6 +161,7 @@ export class PushNotificationManager { /** * 检查并注册设备令牌 + * 每次应用启动都会上报token,更新用户ID绑定关系 */ private async checkAndRegisterToken(token: string): Promise { try { @@ -171,7 +172,8 @@ export class PushNotificationManager { if (!isRegistered || storedToken !== token) { await this.registerDeviceToken(token); } else { - console.log('设备令牌已注册,无需重复注册'); + // 令牌已注册且未变化,更新用户ID绑定关系 + await this.updateTokenUserId(token); } } catch (error) { console.error('检查和注册设备令牌失败:', error); @@ -179,6 +181,47 @@ export class PushNotificationManager { } } + /** + * 更新设备令牌的用户ID绑定关系 + * 用户ID从服务端通过header中的token解析获取 + */ + private async updateTokenUserId(token: string): Promise { + try { + logger.info('更新设备令牌用户ID绑定:', token.substring(0, 20) + '...'); + + // 调用服务端API更新token用户ID绑定关系 + const response = await pushNotificationService.updateTokenUserId(token); + + console.log('设备令牌用户ID绑定更新成功:', response.tokenId); + return true; + } catch (error) { + console.error('更新设备令牌用户ID绑定失败:', error); + // 如果更新用户ID绑定失败,尝试重新注册token + console.log('尝试重新注册设备令牌...'); + return this.registerDeviceToken(token); + } + } + + /** + * 用户登录成功后调用此方法更新token用户ID绑定 + */ + async onUserLogin(): Promise { + try { + const token = this.currentToken || await this.getDeviceToken(); + + if (!token) { + console.warn('未找到设备令牌,无法更新用户ID绑定'); + return false; + } + + return await this.updateTokenUserId(token); + } catch (error) { + console.error('用户登录后更新token用户ID绑定失败:', error); + this.config.onError?.(error as Error); + return false; + } + } + /** * 注册设备令牌到后端 */ diff --git a/services/pushNotifications.ts b/services/pushNotifications.ts index 4f2f0c4..a3f9065 100644 --- a/services/pushNotifications.ts +++ b/services/pushNotifications.ts @@ -19,8 +19,8 @@ export interface DeviceTokenRequest { export interface UpdateTokenRequest { currentDeviceToken: string; newDeviceToken: string; - appVersion: string; - osVersion: string; + appVersion?: string; + osVersion?: string; } // 设备令牌注销请求参数 @@ -125,6 +125,27 @@ export class PushNotificationService { } } + /** + * 更新设备令牌的用户ID绑定关系 + * 用户ID从服务端通过header中的token解析获取 + */ + async updateTokenUserId(deviceToken: string): Promise { + try { + logger.info('更新设备令牌用户ID绑定:', deviceToken.substring(0, 20) + '...'); + + const response = await api.put( + '/push-notifications/update-token-user-id', + { deviceToken } + ); + + logger.info('设备令牌用户ID绑定更新成功:', response.tokenId); + return response; + } catch (error) { + logger.error('更新设备令牌用户ID绑定失败:', error); + throw error; + } + } + /** * 获取本地存储的设备令牌 */ diff --git a/store/userSlice.ts b/store/userSlice.ts index 1e73421..a25a6ba 100644 --- a/store/userSlice.ts +++ b/store/userSlice.ts @@ -1,4 +1,5 @@ import { api, setAuthToken, STORAGE_KEYS } from '@/services/api'; +import { pushNotificationManager } from '@/services/pushNotificationManager'; import { BodyMeasurementsDto, updateBodyMeasurements, updateUser, UpdateUserDto } from '@/services/users'; import AsyncStorage from '@/utils/kvStore'; import { createAsyncThunk, createSelector, createSlice, PayloadAction } from '@reduxjs/toolkit'; @@ -335,6 +336,11 @@ const userSlice = createSlice({ if (!state.profile?.name || !state.profile.name.trim()) { state.profile.name = DEFAULT_MEMBER_NAME; } + + // 登录成功后,更新推送通知token的用户ID绑定 + pushNotificationManager.onUserLogin().catch((error: any) => { + console.error('登录后更新推送通知token用户ID绑定失败:', error); + }); }) .addCase(login.rejected, (state, action) => { state.loading = false;