feat: 添加后台任务调试工具并优化水提醒任务逻辑
This commit is contained in:
@@ -31,10 +31,25 @@ async function executeWaterReminderTask(): Promise<void> {
|
||||
try {
|
||||
console.log('执行喝水提醒后台任务...');
|
||||
|
||||
// 获取当前状态
|
||||
const state = store.getState();
|
||||
const waterStats = state.water.todayStats;
|
||||
const userProfile = state.user.profile;
|
||||
// 获取当前状态,添加错误处理
|
||||
let state;
|
||||
try {
|
||||
state = store.getState();
|
||||
} catch (error) {
|
||||
console.log('无法获取 Redux state,使用本地存储:', error);
|
||||
// 使用本地存储作为后备方案
|
||||
const dailyGoal = await getWaterGoalFromStorage();
|
||||
if (!dailyGoal || dailyGoal <= 0) {
|
||||
console.log('没有设置喝水目标,跳过喝水提醒');
|
||||
return;
|
||||
}
|
||||
// 简化的提醒逻辑
|
||||
await sendSimpleWaterReminder();
|
||||
return;
|
||||
}
|
||||
|
||||
const waterStats = state.water?.todayStats;
|
||||
const userProfile = state.user?.profile;
|
||||
|
||||
// 优先使用 Redux 中的目标,若无则读取本地存储
|
||||
let dailyGoal = waterStats?.dailyGoal ?? 0;
|
||||
@@ -111,9 +126,14 @@ 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 : '朋友';
|
||||
let userName = '朋友';
|
||||
try {
|
||||
const state = store.getState();
|
||||
const normalizedUserName = state.user?.profile?.name?.trim();
|
||||
userName = normalizedUserName && normalizedUserName.length > 0 ? normalizedUserName : '朋友';
|
||||
} catch (error) {
|
||||
console.log('无法获取用户名,使用默认值:', error);
|
||||
}
|
||||
|
||||
const challenges = await listChallenges();
|
||||
const joinedChallenges = challenges.filter((challenge) => challenge.isJoined && challenge.progress);
|
||||
@@ -201,6 +221,33 @@ async function sendTestNotification(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送简单的喝水提醒(后备方案)
|
||||
*/
|
||||
async function sendSimpleWaterReminder(): Promise<void> {
|
||||
try {
|
||||
const userName = '朋友'; // 默认用户名
|
||||
const Notifications = await import('expo-notifications');
|
||||
|
||||
const notificationId = await Notifications.scheduleNotificationAsync({
|
||||
content: {
|
||||
title: '💧 该喝水啦!',
|
||||
body: `${userName},记得补充水分,保持身体健康~`,
|
||||
data: {
|
||||
type: 'water_reminder',
|
||||
url: '/statistics'
|
||||
},
|
||||
sound: 'default',
|
||||
},
|
||||
trigger: null, // 立即发送
|
||||
});
|
||||
|
||||
console.log('简单喝水提醒已发送,ID:', notificationId);
|
||||
} catch (error) {
|
||||
console.error('发送简单喝水提醒失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 后台任务执行函数
|
||||
async function executeBackgroundTasks(): Promise<void> {
|
||||
console.log('开始执行后台任务...');
|
||||
@@ -213,14 +260,30 @@ async function executeBackgroundTasks(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
// await sendTestNotification()
|
||||
// 确保 Redux store 已初始化
|
||||
try {
|
||||
const state = store.getState();
|
||||
if (!state) {
|
||||
console.log('Redux store 未初始化,跳过后台任务');
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('无法访问 Redux store,跳过后台任务:', error);
|
||||
return;
|
||||
}
|
||||
|
||||
// 执行喝水提醒检查任务 - 已禁用,改为由用户手动在设置页面管理
|
||||
// await sendTestNotification() // 可选:启用测试通知
|
||||
|
||||
// 检查是否启用测试通知
|
||||
const testNotificationsEnabled = await AsyncStorage.getItem('@background_test_notifications_enabled') === 'true';
|
||||
if (testNotificationsEnabled) {
|
||||
await sendTestNotification();
|
||||
}
|
||||
|
||||
// 执行喝水提醒检查任务
|
||||
await executeWaterReminderTask();
|
||||
|
||||
// 执行站立提醒检查任务
|
||||
// await executeStandReminderTask();
|
||||
|
||||
// 执行挑战鼓励提醒任务
|
||||
await executeChallengeReminderTask();
|
||||
|
||||
console.log('后台任务执行完成');
|
||||
@@ -269,13 +332,15 @@ export class BackgroundTaskManager {
|
||||
|
||||
if (await TaskManager.isTaskRegisteredAsync(BACKGROUND_TASK_IDENTIFIER)) {
|
||||
log.info('[BackgroundTask] 任务已注册');
|
||||
return
|
||||
} else {
|
||||
log.info('[BackgroundTask] 任务未注册,开始注册...');
|
||||
await BackgroundTask.registerTaskAsync(BACKGROUND_TASK_IDENTIFIER);
|
||||
log.info('[BackgroundTask] 任务注册完成');
|
||||
}
|
||||
|
||||
log.info('[BackgroundTask] 任务未注册, 开始注册...');
|
||||
// 注册后台任务
|
||||
await BackgroundTask.registerTaskAsync(BACKGROUND_TASK_IDENTIFIER);
|
||||
|
||||
// 验证任务状态
|
||||
const status = await BackgroundTask.getStatusAsync();
|
||||
log.info(`[BackgroundTask] 任务状态: ${status}`);
|
||||
|
||||
this.isInitialized = true;
|
||||
log.info('后台任务管理器初始化完成');
|
||||
|
||||
Reference in New Issue
Block a user