This commit is contained in:
richarjiang
2025-09-15 19:28:13 +08:00
parent 281149201b
commit 42b6b2076c
3 changed files with 14 additions and 15 deletions

View File

@@ -1,25 +1,14 @@
import { store } from '@/store';
import AsyncStorage from '@/utils/kvStore';
import { log } from '@/utils/logger';
import { StandReminderHelpers, WaterNotificationHelpers } from '@/utils/notificationHelpers';
import AsyncStorage from '@/utils/kvStore';
import * as BackgroundTask from 'expo-background-task';
import * as TaskManager from 'expo-task-manager';
import { TaskManagerTaskBody } from 'expo-task-manager';
export const BACKGROUND_TASK_IDENTIFIER = 'com.anonymous.digitalpilates.task';
// 定义后台任务
TaskManager.defineTask(BACKGROUND_TASK_IDENTIFIER, async (body: TaskManagerTaskBody) => {
try {
log.info(`[BackgroundTask] 后台任务执行, 任务 ID: ${BACKGROUND_TASK_IDENTIFIER}`);
await executeBackgroundTasks();
} catch (error) {
console.error('[BackgroundTask] 任务执行失败:', error);
return BackgroundTask.BackgroundTaskResult.Failed;
}
return BackgroundTask.BackgroundTaskResult.Success;
});
// 检查通知权限
async function checkNotificationPermissions(): Promise<boolean> {
@@ -191,8 +180,18 @@ export class BackgroundTaskManager {
}
try {
// 定义后台任务
TaskManager.defineTask(BACKGROUND_TASK_IDENTIFIER, async (body: TaskManagerTaskBody) => {
try {
log.info(`[BackgroundTask] 后台任务执行, 任务 ID: ${BACKGROUND_TASK_IDENTIFIER}`);
await executeBackgroundTasks();
} catch (error) {
console.error('[BackgroundTask] 任务执行失败:', error);
return BackgroundTask.BackgroundTaskResult.Failed;
}
return BackgroundTask.BackgroundTaskResult.Success;
});
if (await TaskManager.isTaskRegisteredAsync(BACKGROUND_TASK_IDENTIFIER)) {
log.info('[BackgroundTask] 任务已注册');