fix:更新APP-PUSH

This commit is contained in:
吕新雨
2026-02-10 17:23:38 +08:00
parent 154f347ddb
commit 076bd5636f
4 changed files with 52 additions and 8 deletions

View File

@@ -7,10 +7,10 @@ import { httpJson } from '../utils/http';
import { APP_ENV } from '../constants/env';
import {
getDailyReminderSettings,
getLastRegisteredPushToken,
getLastRegisteredPushPayload,
getOrCreateClientUserId,
getUserProfileScoring,
setLastRegisteredPushToken,
setLastRegisteredPushPayload,
} from '../storage/appStorage';
import type { UserProfileScoring } from '../storage/appStorage';
import { toBackendLocaleFromLanguageTag } from '../i18n/locale';
@@ -170,14 +170,27 @@ export async function ensurePushTokenRegisteredIfPermitted(): Promise<{ ok: bool
const settings = await Notifications.getPermissionsAsync();
if (!isSystemNotificationPermissionGranted(settings.status)) return { ok: false, reason: 'permission_not_granted' };
const clientUserId = await getOrCreateClientUserId();
const token = await getExpoPushTokenOrThrow();
const env = toPushEnv(APP_ENV);
const appId = pickAppId();
// 简单去重token 未变化则不重复上报(减少网络与日志噪音)
const last = await getLastRegisteredPushToken().catch(() => null);
if (last && String(last) === String(token)) return { ok: true, reason: 'already_registered' };
// 去重规则(更严格):
// - 只有当 token + client_user_id + env + app_id 全都一致时才跳过
// - 避免出现“client_user_id 变化但 token 没变,导致后端绑定不更新”的问题
const last = await getLastRegisteredPushPayload().catch(() => null);
if (
last &&
last.pushToken === token &&
last.clientUserId === clientUserId &&
last.env === env &&
last.appId === appId
) {
return { ok: true, reason: 'already_registered' };
}
await registerPushToken({ pushToken: token });
await setLastRegisteredPushToken(token);
await setLastRegisteredPushPayload({ pushToken: token, clientUserId, env, appId });
return { ok: true, reason: 'registered' };
}