feat(challenges): 新增挑战鼓励提醒后台任务与通知支持
- 在 backgroundTaskManager 中增加 executeChallengeReminderTask,每日检查已加入且未打卡的挑战并发送鼓励通知 - 扩展 ChallengeNotificationHelpers 提供 sendEncouragementNotification 方法 - 新增 NotificationTypes.CHALLENGE_ENCOURAGEMENT 及对应点击跳转处理 - challengesApi 补充 checkedInToday 字段用于判断今日是否已打卡 - 临时注释掉挑战列表与详情页头部的礼物/分享按钮,避免干扰主流程
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { store } from '@/store';
|
||||
import AsyncStorage from '@/utils/kvStore';
|
||||
import { log } from '@/utils/logger';
|
||||
import { StandReminderHelpers, WaterNotificationHelpers } from '@/utils/notificationHelpers';
|
||||
import { listChallenges } from '@/services/challengesApi';
|
||||
import { ChallengeNotificationHelpers, StandReminderHelpers, WaterNotificationHelpers } from '@/utils/notificationHelpers';
|
||||
import * as BackgroundTask from 'expo-background-task';
|
||||
import * as TaskManager from 'expo-task-manager';
|
||||
import { TaskManagerTaskBody } from 'expo-task-manager';
|
||||
@@ -99,6 +100,55 @@ async function executeStandReminderTask(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
async function executeChallengeReminderTask(): Promise<void> {
|
||||
try {
|
||||
console.log('执行挑战鼓励提醒后台任务...');
|
||||
|
||||
const state = store.getState();
|
||||
const normalizedUserName = state.user.profile?.name?.trim();
|
||||
const userName = normalizedUserName && normalizedUserName.length > 0 ? normalizedUserName : '朋友';
|
||||
|
||||
const challenges = await listChallenges();
|
||||
const joinedChallenges = challenges.filter((challenge) => challenge.isJoined && challenge.progress);
|
||||
|
||||
if (!joinedChallenges.length) {
|
||||
console.log('没有加入的挑战或挑战没有进度,跳过挑战提醒');
|
||||
return;
|
||||
}
|
||||
|
||||
const todayKey = new Date().toISOString().slice(0, 10);
|
||||
|
||||
for (const challenge of joinedChallenges) {
|
||||
const progress = challenge.progress;
|
||||
if (!progress || progress.checkedInToday) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const storageKey = `@challenge_encouragement_sent:${challenge.id}`;
|
||||
const lastSent = await AsyncStorage.getItem(storageKey);
|
||||
if (lastSent === todayKey) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
await ChallengeNotificationHelpers.sendEncouragementNotification({
|
||||
userName,
|
||||
challengeTitle: challenge.title,
|
||||
challengeId: challenge.id,
|
||||
});
|
||||
|
||||
await AsyncStorage.setItem(storageKey, todayKey);
|
||||
} catch (notificationError) {
|
||||
console.error('发送挑战鼓励通知失败:', notificationError);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('挑战鼓励提醒后台任务完成');
|
||||
} catch (error) {
|
||||
console.error('执行挑战鼓励提醒后台任务失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 发送测试通知以验证后台任务执行
|
||||
async function sendTestNotification(): Promise<void> {
|
||||
try {
|
||||
@@ -149,6 +199,8 @@ async function executeBackgroundTasks(): Promise<void> {
|
||||
// 执行站立提醒检查任务
|
||||
// await executeStandReminderTask();
|
||||
|
||||
await executeChallengeReminderTask();
|
||||
|
||||
console.log('后台任务执行完成');
|
||||
} catch (error) {
|
||||
console.error('执行后台任务失败:', error);
|
||||
|
||||
Reference in New Issue
Block a user