Refactor: Remove background task management and related hooks

- Deleted `useBackgroundTasks.ts` hook and its associated logic for managing background tasks.
- Removed `backgroundTaskManager.ts` service and all related task definitions and registrations.
- Cleaned up `Podfile.lock` and `package.json` to remove unused dependencies related to background tasks.
- Updated iOS project files to eliminate references to removed background task components.
- Added new background fetch identifier in `Info.plist` for future use.
This commit is contained in:
richarjiang
2025-09-05 09:47:49 +08:00
parent cb89ee7bc2
commit acb3907344
60 changed files with 77 additions and 2230 deletions

View File

@@ -1,374 +0,0 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as BackgroundTask from 'expo-background-task';
import * as TaskManager from 'expo-task-manager';
// 后台任务名称常量
const BACKGROUND_TASK_NAME = 'health-background-task';
// 必须在全局作用域定义任务处理器
TaskManager.defineTask(BACKGROUND_TASK_NAME, async () => {
console.log('======= 后台任务被系统调用 =======');
const now = new Date();
console.log(`后台任务执行时间: ${now.toISOString()}`);
try {
// 获取后台任务管理器实例并执行所有注册的任务
const manager = BackgroundTaskManager.getInstance();
console.log('开始执行所有注册的任务...');
const results = await manager.executeAllTasks();
console.log('后台任务执行结果:', results);
// 返回成功状态
return BackgroundTask.BackgroundTaskResult.Success;
} catch (error) {
console.error('后台任务执行失败:', error);
return BackgroundTask.BackgroundTaskResult.Failed;
}
});
// 任务类型定义
export interface BackgroundTaskConfig {
id: string;
name: string;
handler: (data?: any) => Promise<void>;
options?: {
minimumInterval?: number; // 最小间隔时间(分钟)
};
}
// 任务状态
export interface TaskStatus {
id: string;
isRegistered: boolean;
lastExecution?: Date;
nextExecution?: Date;
executionCount: number;
lastError?: string;
}
// 后台任务管理器类
class BackgroundTaskManager {
private static instance: BackgroundTaskManager;
private tasks: Map<string, BackgroundTaskConfig> = new Map();
private taskStatuses: Map<string, TaskStatus> = new Map();
private isInitialized = false;
private systemTaskRegistered = false;
// 单例模式
public static getInstance(): BackgroundTaskManager {
if (!BackgroundTaskManager.instance) {
BackgroundTaskManager.instance = new BackgroundTaskManager();
}
return BackgroundTaskManager.instance;
}
// 初始化后台任务管理器
public async initialize(): Promise<void> {
if (this.isInitialized) {
return;
}
try {
this.isInitialized = true;
// 注册后台任务
await this.registerSystemBackgroundTask();
// 加载已保存的任务状态
await this.loadTaskStatuses();
console.log('后台任务管理器初始化成功');
} catch (error) {
console.error('后台任务管理器初始化失败:', error);
throw error;
}
}
// 注册系统后台任务
private async registerSystemBackgroundTask(): Promise<void> {
console.log('开始注册系统后台任务...');
try {
// 检查后台任务状态
const status = await BackgroundTask.getStatusAsync();
console.log('后台任务服务状态:', BackgroundTask.BackgroundTaskStatus[status]);
if (status === BackgroundTask.BackgroundTaskStatus.Available) {
// 检查任务是否已经注册
const isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_TASK_NAME);
console.log('系统任务是否已注册:', isRegistered);
if (!isRegistered) {
// 注册后台任务
await BackgroundTask.registerTaskAsync(BACKGROUND_TASK_NAME);
console.log('✅ 系统后台任务注册成功');
this.systemTaskRegistered = true;
} else {
console.log('✅ 系统后台任务已经注册,跳过重复注册');
this.systemTaskRegistered = true;
}
} else {
const statusText = Object.keys(BackgroundTask.BackgroundTaskStatus).find(
key => BackgroundTask.BackgroundTaskStatus[key as keyof typeof BackgroundTask.BackgroundTaskStatus] === status
);
console.warn('❌ 后台任务服务不可用,状态:', statusText || status);
console.warn('可能的原因:');
console.warn('- 设备省电模式开启');
console.warn('- 后台应用刷新被禁用');
console.warn('- 设备电量过低');
this.systemTaskRegistered = false;
}
} catch (error) {
console.error('❌ 注册系统后台任务失败:', error);
this.systemTaskRegistered = false;
throw error;
}
}
// 注册自定义任务
public async registerTask(task: BackgroundTaskConfig): Promise<void> {
try {
// 检查任务是否已存在
if (this.tasks.has(task.id)) {
console.warn(`任务 ${task.id} 已存在,将被覆盖`);
}
// 保存任务
this.tasks.set(task.id, task);
// 初始化任务状态
if (!this.taskStatuses.has(task.id)) {
this.taskStatuses.set(task.id, {
id: task.id,
isRegistered: true,
executionCount: 0,
});
}
// 保存任务状态
await this.saveTaskStatuses();
console.log(`任务 ${task.id} 注册成功`);
} catch (error) {
console.error(`注册任务 ${task.id} 失败:`, error);
throw error;
}
}
// 取消注册任务
public async unregisterTask(taskId: string): Promise<void> {
try {
// 移除任务
this.tasks.delete(taskId);
// 更新任务状态
const status = this.taskStatuses.get(taskId);
if (status) {
status.isRegistered = false;
await this.saveTaskStatuses();
}
console.log(`任务 ${taskId} 取消注册成功`);
} catch (error) {
console.error(`取消注册任务 ${taskId} 失败:`, error);
throw error;
}
}
// 手动执行任务
public async executeTask(taskId: string, data?: any): Promise<void> {
try {
const task = this.tasks.get(taskId);
if (!task) {
throw new Error(`任务 ${taskId} 不存在`);
}
console.log(`开始执行任务: ${taskId}`);
// 执行任务
await task.handler(data);
// 更新任务状态
const status = this.taskStatuses.get(taskId);
if (status) {
status.lastExecution = new Date();
status.executionCount += 1;
status.lastError = undefined;
await this.saveTaskStatuses();
}
console.log(`任务 ${taskId} 执行成功`);
} catch (error) {
console.error(`执行任务 ${taskId} 失败:`, error);
// 更新错误状态
const status = this.taskStatuses.get(taskId);
if (status) {
status.lastError = error instanceof Error ? error.message : String(error);
await this.saveTaskStatuses();
}
throw error;
}
}
// 执行所有任务
public async executeAllTasks(): Promise<{ [taskId: string]: 'success' | 'failed' }> {
const results: { [taskId: string]: 'success' | 'failed' } = {};
for (const [taskId, task] of Array.from(this.tasks.entries())) {
try {
await this.executeTask(taskId);
results[taskId] = 'success';
} catch (error) {
console.error(`执行任务 ${taskId} 失败:`, error);
results[taskId] = 'failed';
}
}
return results;
}
// 获取任务状态
public getTaskStatus(taskId: string): TaskStatus | undefined {
return this.taskStatuses.get(taskId);
}
// 获取所有任务状态
public getAllTaskStatuses(): TaskStatus[] {
return Array.from(this.taskStatuses.values());
}
// 获取已注册的任务列表
public getRegisteredTasks(): BackgroundTaskConfig[] {
return Array.from(this.tasks.values());
}
// 检查后台任务状态
public async getBackgroundTaskStatus(): Promise<BackgroundTask.BackgroundTaskStatus> {
return await BackgroundTask.getStatusAsync();
}
// 保存任务状态到本地存储
private async saveTaskStatuses(): Promise<void> {
try {
const statuses = Array.from(this.taskStatuses.values());
await AsyncStorage.setItem('@background_task_statuses', JSON.stringify(statuses));
} catch (error) {
console.error('保存任务状态失败:', error);
}
}
// 从本地存储加载任务状态
private async loadTaskStatuses(): Promise<void> {
try {
const statusesJson = await AsyncStorage.getItem('@background_task_statuses');
if (statusesJson) {
const statuses: TaskStatus[] = JSON.parse(statusesJson);
statuses.forEach(status => {
this.taskStatuses.set(status.id, status);
});
}
} catch (error) {
console.error('加载任务状态失败:', error);
}
}
// 清理过期的任务状态
public async cleanupTaskStatuses(): Promise<void> {
const now = new Date();
const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
for (const [taskId, status] of this.taskStatuses) {
if (status.lastExecution && status.lastExecution < thirtyDaysAgo && !status.isRegistered) {
this.taskStatuses.delete(taskId);
}
}
await this.saveTaskStatuses();
}
// 手动触发后台任务(仅开发环境)
public async triggerTaskForTesting(): Promise<void> {
if (!__DEV__) {
console.warn('⚠️ triggerTaskForTesting 仅在开发环境可用');
return;
}
try {
console.log('🧪 触发后台任务进行测试...');
await BackgroundTask.triggerTaskWorkerForTestingAsync();
console.log('✅ 后台任务测试触发成功');
} catch (error) {
console.error('❌ 触发后台任务测试失败:', error);
}
}
// 调试函数:显示后台任务状态
public async debugExecuteBackgroundTask(): Promise<void> {
console.log('===============================');
console.log('🔧 调试:后台任务状态检查');
console.log('===============================');
try {
// 获取后台任务状态
const status = await this.getBackgroundTaskStatus();
const statusText = Object.keys(BackgroundTask.BackgroundTaskStatus).find(
key => BackgroundTask.BackgroundTaskStatus[key as keyof typeof BackgroundTask.BackgroundTaskStatus] === status
);
console.log('📊 后台任务服务状态:', statusText || status);
// 检查系统任务是否已注册
const isSystemTaskRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_TASK_NAME);
console.log('🔄 系统后台任务是否已注册:', isSystemTaskRegistered);
console.log('🔄 管理器中的系统任务状态:', this.systemTaskRegistered);
// 显示自定义任务信息
console.log('📝 当前注册的自定义任务数量:', this.tasks.size);
this.tasks.forEach((task, id) => {
console.log(` - 任务ID: ${id}, 名称: ${task.name}`);
});
if (this.tasks.size === 0) {
console.warn('⚠️ 没有注册的自定义任务');
}
// 手动执行所有任务
console.log('🚀 手动执行所有注册的任务...');
const results = await this.executeAllTasks();
console.log('✅ 任务执行结果:', results);
// 显示详细的任务状态
console.log('📈 任务执行统计:');
const taskStatuses = this.getAllTaskStatuses();
taskStatuses.forEach(taskStatus => {
console.log(` 📊 任务 ${taskStatus.id}:`, {
注册状态: taskStatus.isRegistered ? '✅ 已注册' : '❌ 未注册',
执行次数: taskStatus.executionCount,
最后执行: taskStatus.lastExecution?.toLocaleString('zh-CN') || '从未执行',
最后错误: taskStatus.lastError || '无'
});
});
// 开发环境下触发测试
if (__DEV__) {
console.log('🧪 开发环境:触发后台任务测试...');
await this.triggerTaskForTesting();
}
console.log('===============================');
console.log('✅ 调试检查完成');
console.log('===============================');
} catch (error) {
console.error('❌ 调试执行失败:', error);
console.log('===============================');
}
}
}
// 导出单例实例
export const backgroundTaskManager = BackgroundTaskManager.getInstance();
// 导出类型
export type { BackgroundTaskConfig as BackgroundTaskType, TaskStatus as TaskStatusType };

View File

@@ -1,253 +0,0 @@
import { BackgroundTaskType as BackgroundTask, backgroundTaskManager } from './backgroundTaskManager';
// 示例任务:数据同步任务
export const createDataSyncTask = (): BackgroundTask => ({
id: 'data-sync-task',
name: '数据同步任务',
handler: async (data?: any) => {
console.log('开始执行数据同步任务');
try {
// 这里实现您的数据同步逻辑
// 例如:同步用户数据、运动记录、目标进度等
// 模拟数据同步过程
await new Promise(resolve => setTimeout(resolve, 2000));
console.log('数据同步任务执行完成');
} catch (error) {
console.error('数据同步任务执行失败:', error);
throw error;
}
},
options: {
minimumInterval: 5, // 5分钟最小间隔
stopOnTerminate: false,
startOnBoot: true,
},
});
// 示例任务:健康数据更新任务
export const createHealthDataUpdateTask = (): BackgroundTask => ({
id: 'health-data-update-task',
name: '健康数据更新任务',
handler: async (data?: any) => {
console.log('开始执行健康数据更新任务');
try {
// 这里实现您的健康数据更新逻辑
// 例如:更新步数、心率、体重等健康数据
// 模拟健康数据更新过程
await new Promise(resolve => setTimeout(resolve, 1500));
console.log('健康数据更新任务执行完成');
} catch (error) {
console.error('健康数据更新任务执行失败:', error);
throw error;
}
},
options: {
minimumInterval: 10, // 10分钟最小间隔
stopOnTerminate: false,
startOnBoot: true,
},
});
// 示例任务:通知检查任务
export const createNotificationCheckTask = (): BackgroundTask => ({
id: 'notification-check-task',
name: '通知检查任务',
handler: async (data?: any) => {
console.log('开始执行通知检查任务');
try {
// 这里实现您的通知检查逻辑
// 例如:检查是否需要发送运动提醒、目标达成通知等
// 模拟通知检查过程
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('通知检查任务执行完成');
} catch (error) {
console.error('通知检查任务执行失败:', error);
throw error;
}
},
options: {
minimumInterval: 30, // 30分钟最小间隔
stopOnTerminate: false,
startOnBoot: true,
},
});
// 喝水提醒检查任务
export const createWaterReminderTask = (): BackgroundTask => ({
id: 'water-reminder-task',
name: '喝水提醒检查任务',
handler: async (data?: any) => {
console.log('开始执行喝水提醒检查任务');
try {
// 导入必要的模块
const { WaterNotificationHelpers } = await import('@/utils/notificationHelpers');
const { getTodayWaterStats } = await import('@/services/waterRecords');
const AsyncStorage = (await import('@react-native-async-storage/async-storage')).default;
// 获取用户信息
const userProfileJson = await AsyncStorage.getItem('@user_profile');
const userProfile = userProfileJson ? JSON.parse(userProfileJson) : null;
const userName = userProfile?.name || '朋友';
// 检查时间限制早上9点以前和晚上9点以后不通知
const currentHour = new Date().getHours();
if (currentHour < 9 || currentHour >= 21) {
console.log(`当前时间${currentHour}不在通知时间范围内9:00-21:00跳过喝水提醒检查`);
return;
}
// 获取今日喝水统计数据
let todayStats;
try {
todayStats = await getTodayWaterStats();
} catch (error) {
console.log('获取喝水统计数据失败,可能用户未登录或无网络连接:', error);
return;
}
if (!todayStats || !todayStats.dailyGoal || todayStats.dailyGoal <= 0) {
console.log('没有设置喝水目标或目标无效,跳过喝水检查');
return;
}
// 构造今日统计数据
const waterStatsForCheck = {
totalAmount: todayStats.totalAmount || 0,
dailyGoal: todayStats.dailyGoal,
completionRate: todayStats.completionRate || 0
};
// 调用喝水通知检查函数
const notificationSent = await WaterNotificationHelpers.checkWaterGoalAndNotify(
userName,
waterStatsForCheck,
currentHour
);
if (notificationSent) {
console.log('喝水提醒通知已发送');
} else {
console.log('无需发送喝水提醒通知');
}
console.log('喝水提醒检查任务执行完成');
} catch (error) {
console.error('喝水提醒检查任务执行失败:', error);
throw error;
}
},
options: {
minimumInterval: 60, // 60分钟最小间隔
stopOnTerminate: false,
startOnBoot: true,
},
});
// 示例任务:缓存清理任务
export const createCacheCleanupTask = (): BackgroundTask => ({
id: 'cache-cleanup-task',
name: '缓存清理任务',
handler: async (data?: any) => {
console.log('开始执行缓存清理任务');
try {
// 这里实现您的缓存清理逻辑
// 例如:清理过期的图片缓存、临时文件等
// 模拟缓存清理过程
await new Promise(resolve => setTimeout(resolve, 3000));
console.log('缓存清理任务执行完成');
} catch (error) {
console.error('缓存清理任务执行失败:', error);
throw error;
}
},
options: {
minimumInterval: 86400, // 24小时最小间隔
stopOnTerminate: false,
startOnBoot: true,
},
});
// 示例任务:用户行为分析任务
export const createUserAnalyticsTask = (): BackgroundTask => ({
id: 'user-analytics-task',
name: '用户行为分析任务',
handler: async (data?: any) => {
console.log('开始执行用户行为分析任务');
try {
// 这里实现您的用户行为分析逻辑
// 例如:分析用户运动习惯、使用模式等
// 模拟用户行为分析过程
await new Promise(resolve => setTimeout(resolve, 2500));
console.log('用户行为分析任务执行完成');
} catch (error) {
console.error('用户行为分析任务执行失败:', error);
throw error;
}
},
options: {
minimumInterval: 60, // 1小时最小间隔
stopOnTerminate: false,
startOnBoot: true,
},
});
// 注册所有默认任务
export const registerDefaultTasks = async (): Promise<void> => {
try {
const tasks = [
createDataSyncTask(),
createHealthDataUpdateTask(),
createNotificationCheckTask(),
createWaterReminderTask(),
createCacheCleanupTask(),
createUserAnalyticsTask(),
];
for (const task of tasks) {
await backgroundTaskManager.registerTask(task);
}
console.log('所有默认任务注册完成');
} catch (error) {
console.error('注册默认任务失败:', error);
throw error;
}
};
// 创建自定义任务的工厂函数
export const createCustomTask = (
id: string,
name: string,
handler: (data?: any) => Promise<void>,
options?: {
minimumInterval?: number;
stopOnTerminate?: boolean;
startOnBoot?: boolean;
}
): BackgroundTask => ({
id,
name,
handler,
options: {
minimumInterval: 300, // 默认5分钟
stopOnTerminate: false,
startOnBoot: true,
...options,
},
});