2026-03-09 21:29:03 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 后台保活服务
|
|
|
|
|
|
* 使用 expo-background-fetch 和 expo-task-manager 实现 App 后台保活
|
|
|
|
|
|
*
|
|
|
|
|
|
* 功能:
|
|
|
|
|
|
* - 定期后台任务保持 App 在内存中
|
2026-03-19 00:56:41 +08:00
|
|
|
|
* - 配合 SSE 服务保持长连接
|
2026-03-09 21:29:03 +08:00
|
|
|
|
* - 收到消息时触发震动提示
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import { AppState, AppStateStatus, Platform } from 'react-native';
|
|
|
|
|
|
import * as BackgroundFetch from 'expo-background-fetch';
|
|
|
|
|
|
import * as TaskManager from 'expo-task-manager';
|
2026-03-10 12:58:23 +08:00
|
|
|
|
import { sseService } from './sseService';
|
2026-03-24 14:21:31 +08:00
|
|
|
|
import {
|
|
|
|
|
|
triggerVibration,
|
|
|
|
|
|
vibrateOnMessage,
|
|
|
|
|
|
setVibrationConfig,
|
|
|
|
|
|
getVibrationConfig,
|
|
|
|
|
|
setVibrationEnabled,
|
|
|
|
|
|
} from './messageVibrationService';
|
|
|
|
|
|
export { triggerVibration, vibrateOnMessage, setVibrationConfig, getVibrationConfig, setVibrationEnabled };
|
2026-03-09 21:29:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 后台任务名称
|
|
|
|
|
|
const BACKGROUND_FETCH_TASK = 'background-fetch-keepalive';
|
2026-03-10 12:58:23 +08:00
|
|
|
|
const REALTIME_KEEPALIVE_TASK = 'realtime-keepalive';
|
2026-03-09 21:29:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 后台任务间隔(Android 最小 15 分钟,iOS 最小 15 分钟)
|
|
|
|
|
|
const BACKGROUND_INTERVAL = 15; // 15 分钟
|
|
|
|
|
|
|
|
|
|
|
|
// 后台服务状态
|
|
|
|
|
|
let isInitialized = false;
|
|
|
|
|
|
let appStateSubscription: any = null;
|
|
|
|
|
|
|
|
|
|
|
|
// 定义后台任务
|
|
|
|
|
|
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
|
|
|
|
|
|
try {
|
2026-03-19 00:56:41 +08:00
|
|
|
|
// 检查 SSE 连接状态
|
2026-03-10 12:58:23 +08:00
|
|
|
|
if (!sseService.isConnected()) {
|
|
|
|
|
|
await sseService.connect();
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 返回收到新数据
|
|
|
|
|
|
return BackgroundFetch.BackgroundFetchResult.NewData;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[BackgroundService] 后台任务执行失败:', error);
|
|
|
|
|
|
return BackgroundFetch.BackgroundFetchResult.Failed;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-03-19 00:56:41 +08:00
|
|
|
|
// SSE 保活任务
|
2026-03-10 12:58:23 +08:00
|
|
|
|
TaskManager.defineTask(REALTIME_KEEPALIVE_TASK, async () => {
|
2026-03-09 21:29:03 +08:00
|
|
|
|
try {
|
2026-03-10 12:58:23 +08:00
|
|
|
|
if (!sseService.isConnected()) {
|
|
|
|
|
|
await sseService.connect();
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
return BackgroundFetch.BackgroundFetchResult.NewData;
|
|
|
|
|
|
} catch (error) {
|
2026-03-10 12:58:23 +08:00
|
|
|
|
console.error('[BackgroundService] SSE 保活失败:', error);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
return BackgroundFetch.BackgroundFetchResult.Failed;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 注册后台任务
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function registerBackgroundTasks(): Promise<void> {
|
2026-03-24 14:21:31 +08:00
|
|
|
|
if (Platform.OS === 'web') {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-03-09 21:29:03 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// 检查是否已注册
|
|
|
|
|
|
const isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_FETCH_TASK);
|
|
|
|
|
|
if (!isRegistered) {
|
|
|
|
|
|
await BackgroundFetch.registerTaskAsync(BACKGROUND_FETCH_TASK, {
|
|
|
|
|
|
minimumInterval: BACKGROUND_INTERVAL * 60, // 转换为秒
|
|
|
|
|
|
stopOnTerminate: false, // App 终止后继续运行
|
|
|
|
|
|
startOnBoot: true, // 设备启动后自动运行
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-19 00:56:41 +08:00
|
|
|
|
// 注册 SSE 保活任务
|
|
|
|
|
|
const isSseKeepaliveRegistered = await TaskManager.isTaskRegisteredAsync(REALTIME_KEEPALIVE_TASK);
|
|
|
|
|
|
if (!isSseKeepaliveRegistered) {
|
2026-03-10 12:58:23 +08:00
|
|
|
|
await BackgroundFetch.registerTaskAsync(REALTIME_KEEPALIVE_TASK, {
|
2026-03-09 21:29:03 +08:00
|
|
|
|
minimumInterval: 60, // 1 分钟检查一次
|
|
|
|
|
|
stopOnTerminate: false,
|
|
|
|
|
|
startOnBoot: true,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[BackgroundService] 注册后台任务失败:', error);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 取消后台任务
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function unregisterBackgroundTasks(): Promise<void> {
|
2026-03-24 14:21:31 +08:00
|
|
|
|
if (Platform.OS === 'web') {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-03-09 21:29:03 +08:00
|
|
|
|
try {
|
|
|
|
|
|
await BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK);
|
2026-03-10 12:58:23 +08:00
|
|
|
|
await BackgroundFetch.unregisterTaskAsync(REALTIME_KEEPALIVE_TASK);
|
2026-03-09 21:29:03 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[BackgroundService] 取消后台任务失败:', error);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 设置 App 状态监听
|
|
|
|
|
|
*/
|
|
|
|
|
|
function setupAppStateListener(): void {
|
|
|
|
|
|
if (appStateSubscription) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
appStateSubscription = AppState.addEventListener('change', (nextAppState: AppStateStatus) => {
|
2026-03-09 22:18:47 +08:00
|
|
|
|
void nextAppState;
|
2026-03-09 21:29:03 +08:00
|
|
|
|
if (nextAppState === 'active') {
|
|
|
|
|
|
// App 回到前台,确保连接
|
2026-03-10 12:58:23 +08:00
|
|
|
|
if (!sseService.isConnected()) {
|
|
|
|
|
|
sseService.connect();
|
2026-03-09 21:29:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 初始化后台保活服务
|
|
|
|
|
|
*/
|
|
|
|
|
|
export async function initBackgroundService(): Promise<boolean> {
|
|
|
|
|
|
if (isInitialized) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-24 14:21:31 +08:00
|
|
|
|
if (Platform.OS === 'web') {
|
|
|
|
|
|
setupAppStateListener();
|
|
|
|
|
|
isInitialized = true;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-09 21:29:03 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// 检查后台任务状态
|
|
|
|
|
|
const status = await BackgroundFetch.getStatusAsync();
|
|
|
|
|
|
if (status !== BackgroundFetch.BackgroundFetchStatus.Available) {
|
|
|
|
|
|
console.warn('[BackgroundService] 后台任务不可用,状态:', status);
|
|
|
|
|
|
// 即使后台任务不可用,也继续初始化其他功能
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 注册后台任务
|
|
|
|
|
|
await registerBackgroundTasks();
|
|
|
|
|
|
|
|
|
|
|
|
// 设置 App 状态监听
|
|
|
|
|
|
setupAppStateListener();
|
|
|
|
|
|
|
|
|
|
|
|
isInitialized = true;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[BackgroundService] 初始化失败:', error);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 停止后台保活服务
|
|
|
|
|
|
*/
|
|
|
|
|
|
export async function stopBackgroundService(): Promise<void> {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await unregisterBackgroundTasks();
|
|
|
|
|
|
|
|
|
|
|
|
if (appStateSubscription) {
|
|
|
|
|
|
appStateSubscription.remove();
|
|
|
|
|
|
appStateSubscription = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
isInitialized = false;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[BackgroundService] 停止服务失败:', error);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 检查后台服务状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
export async function checkBackgroundStatus(): Promise<{
|
|
|
|
|
|
isAvailable: boolean;
|
|
|
|
|
|
isInitialized: boolean;
|
|
|
|
|
|
registeredTasks: string[];
|
|
|
|
|
|
}> {
|
2026-03-24 14:21:31 +08:00
|
|
|
|
if (Platform.OS === 'web') {
|
|
|
|
|
|
return {
|
|
|
|
|
|
isAvailable: false,
|
|
|
|
|
|
isInitialized,
|
|
|
|
|
|
registeredTasks: [],
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2026-03-09 21:29:03 +08:00
|
|
|
|
const status = await BackgroundFetch.getStatusAsync();
|
|
|
|
|
|
const registeredTasks = await TaskManager.getRegisteredTasksAsync();
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
isAvailable: status === BackgroundFetch.BackgroundFetchStatus.Available,
|
|
|
|
|
|
isInitialized,
|
|
|
|
|
|
registeredTasks: registeredTasks.map(task => task.taskName),
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 导出服务实例
|
|
|
|
|
|
export const backgroundService = {
|
|
|
|
|
|
init: initBackgroundService,
|
|
|
|
|
|
stop: stopBackgroundService,
|
|
|
|
|
|
vibrate: triggerVibration,
|
|
|
|
|
|
vibrateOnMessage,
|
|
|
|
|
|
setVibrationConfig,
|
|
|
|
|
|
getVibrationConfig,
|
|
|
|
|
|
setVibrationEnabled,
|
|
|
|
|
|
checkStatus: checkBackgroundStatus,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default backgroundService;
|