From fbffa07f744ed525ec2335f448e83a20d95303d1 Mon Sep 17 00:00:00 2001 From: richarjiang Date: Mon, 3 Nov 2025 17:58:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(push-notification):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=E5=90=8E=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8E=A8=E9=80=81token=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增updateTokenUserId方法用于更新设备令牌的用户ID绑定关系 - 添加onUserLogin方法在用户登录成功后自动更新token绑定 - 优化checkAndRegisterToken逻辑,确保每次应用启动都更新用户ID绑定 - 修改UpdateTokenRequest接口,将appVersion和osVersion设为可选参数 - 在用户登录成功后自动触发推送token用户ID绑定更新 --- ios/OutLive/Info.plist | 2 +- services/pushNotificationManager.ts | 45 ++++++++++++++++++++++++++++- services/pushNotifications.ts | 25 ++++++++++++++-- store/userSlice.ts | 6 ++++ 4 files changed, 74 insertions(+), 4 deletions(-) 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;