feat(push-notification): 添加用户登录后自动更新推送token绑定功能
- 新增updateTokenUserId方法用于更新设备令牌的用户ID绑定关系 - 添加onUserLogin方法在用户登录成功后自动更新token绑定 - 优化checkAndRegisterToken逻辑,确保每次应用启动都更新用户ID绑定 - 修改UpdateTokenRequest接口,将appVersion和osVersion设为可选参数 - 在用户登录成功后自动触发推送token用户ID绑定更新
This commit is contained in:
@@ -26,7 +26,7 @@
|
|||||||
<key>CFBundlePackageType</key>
|
<key>CFBundlePackageType</key>
|
||||||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
|
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
|
||||||
<key>CFBundleShortVersionString</key>
|
<key>CFBundleShortVersionString</key>
|
||||||
<string>1.0.22</string>
|
<string>1.0.23</string>
|
||||||
<key>CFBundleSignature</key>
|
<key>CFBundleSignature</key>
|
||||||
<string>????</string>
|
<string>????</string>
|
||||||
<key>CFBundleURLTypes</key>
|
<key>CFBundleURLTypes</key>
|
||||||
|
|||||||
@@ -161,6 +161,7 @@ export class PushNotificationManager {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查并注册设备令牌
|
* 检查并注册设备令牌
|
||||||
|
* 每次应用启动都会上报token,更新用户ID绑定关系
|
||||||
*/
|
*/
|
||||||
private async checkAndRegisterToken(token: string): Promise<void> {
|
private async checkAndRegisterToken(token: string): Promise<void> {
|
||||||
try {
|
try {
|
||||||
@@ -171,7 +172,8 @@ export class PushNotificationManager {
|
|||||||
if (!isRegistered || storedToken !== token) {
|
if (!isRegistered || storedToken !== token) {
|
||||||
await this.registerDeviceToken(token);
|
await this.registerDeviceToken(token);
|
||||||
} else {
|
} else {
|
||||||
console.log('设备令牌已注册,无需重复注册');
|
// 令牌已注册且未变化,更新用户ID绑定关系
|
||||||
|
await this.updateTokenUserId(token);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('检查和注册设备令牌失败:', error);
|
console.error('检查和注册设备令牌失败:', error);
|
||||||
@@ -179,6 +181,47 @@ export class PushNotificationManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新设备令牌的用户ID绑定关系
|
||||||
|
* 用户ID从服务端通过header中的token解析获取
|
||||||
|
*/
|
||||||
|
private async updateTokenUserId(token: string): Promise<boolean> {
|
||||||
|
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<boolean> {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册设备令牌到后端
|
* 注册设备令牌到后端
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ export interface DeviceTokenRequest {
|
|||||||
export interface UpdateTokenRequest {
|
export interface UpdateTokenRequest {
|
||||||
currentDeviceToken: string;
|
currentDeviceToken: string;
|
||||||
newDeviceToken: string;
|
newDeviceToken: string;
|
||||||
appVersion: string;
|
appVersion?: string;
|
||||||
osVersion: string;
|
osVersion?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设备令牌注销请求参数
|
// 设备令牌注销请求参数
|
||||||
@@ -125,6 +125,27 @@ export class PushNotificationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新设备令牌的用户ID绑定关系
|
||||||
|
* 用户ID从服务端通过header中的token解析获取
|
||||||
|
*/
|
||||||
|
async updateTokenUserId(deviceToken: string): Promise<PushNotificationResponse> {
|
||||||
|
try {
|
||||||
|
logger.info('更新设备令牌用户ID绑定:', deviceToken.substring(0, 20) + '...');
|
||||||
|
|
||||||
|
const response = await api.put<PushNotificationResponse>(
|
||||||
|
'/push-notifications/update-token-user-id',
|
||||||
|
{ deviceToken }
|
||||||
|
);
|
||||||
|
|
||||||
|
logger.info('设备令牌用户ID绑定更新成功:', response.tokenId);
|
||||||
|
return response;
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('更新设备令牌用户ID绑定失败:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取本地存储的设备令牌
|
* 获取本地存储的设备令牌
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { api, setAuthToken, STORAGE_KEYS } from '@/services/api';
|
import { api, setAuthToken, STORAGE_KEYS } from '@/services/api';
|
||||||
|
import { pushNotificationManager } from '@/services/pushNotificationManager';
|
||||||
import { BodyMeasurementsDto, updateBodyMeasurements, updateUser, UpdateUserDto } from '@/services/users';
|
import { BodyMeasurementsDto, updateBodyMeasurements, updateUser, UpdateUserDto } from '@/services/users';
|
||||||
import AsyncStorage from '@/utils/kvStore';
|
import AsyncStorage from '@/utils/kvStore';
|
||||||
import { createAsyncThunk, createSelector, createSlice, PayloadAction } from '@reduxjs/toolkit';
|
import { createAsyncThunk, createSelector, createSlice, PayloadAction } from '@reduxjs/toolkit';
|
||||||
@@ -335,6 +336,11 @@ const userSlice = createSlice({
|
|||||||
if (!state.profile?.name || !state.profile.name.trim()) {
|
if (!state.profile?.name || !state.profile.name.trim()) {
|
||||||
state.profile.name = DEFAULT_MEMBER_NAME;
|
state.profile.name = DEFAULT_MEMBER_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 登录成功后,更新推送通知token的用户ID绑定
|
||||||
|
pushNotificationManager.onUserLogin().catch((error: any) => {
|
||||||
|
console.error('登录后更新推送通知token用户ID绑定失败:', error);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.addCase(login.rejected, (state, action) => {
|
.addCase(login.rejected, (state, action) => {
|
||||||
state.loading = false;
|
state.loading = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user