Initial frontend repository commit.
Include app source and update .gitignore to exclude local release artifacts and signing files. Made-with: Cursor
This commit is contained in:
319
src/services/backgroundService.ts
Normal file
319
src/services/backgroundService.ts
Normal file
@@ -0,0 +1,319 @@
|
||||
/**
|
||||
* 后台保活服务
|
||||
* 使用 expo-background-fetch 和 expo-task-manager 实现 App 后台保活
|
||||
*
|
||||
* 功能:
|
||||
* - 定期后台任务保持 App 在内存中
|
||||
* - 配合 WebSocket 服务保持长连接
|
||||
* - 收到消息时触发震动提示
|
||||
*/
|
||||
|
||||
import { AppState, AppStateStatus, Platform } from 'react-native';
|
||||
import * as BackgroundFetch from 'expo-background-fetch';
|
||||
import * as TaskManager from 'expo-task-manager';
|
||||
import * as Haptics from 'expo-haptics';
|
||||
import { websocketService } from './websocketService';
|
||||
|
||||
// 后台任务名称
|
||||
const BACKGROUND_FETCH_TASK = 'background-fetch-keepalive';
|
||||
const WEBSOCKET_KEEPALIVE_TASK = 'websocket-keepalive';
|
||||
|
||||
// 后台任务间隔(Android 最小 15 分钟,iOS 最小 15 分钟)
|
||||
const BACKGROUND_INTERVAL = 15; // 15 分钟
|
||||
|
||||
// 震动配置
|
||||
interface VibrationConfig {
|
||||
enabled: boolean; // 是否启用震动
|
||||
onChatMessage: boolean; // 私聊消息震动
|
||||
onGroupMessage: boolean; // 群聊消息震动
|
||||
onNotification: boolean; // 系统通知震动
|
||||
style: Haptics.ImpactFeedbackStyle; // 震动样式
|
||||
}
|
||||
|
||||
// 默认震动配置
|
||||
const defaultVibrationConfig: VibrationConfig = {
|
||||
enabled: true,
|
||||
onChatMessage: true,
|
||||
onGroupMessage: true,
|
||||
onNotification: true,
|
||||
style: Haptics.ImpactFeedbackStyle.Light,
|
||||
};
|
||||
|
||||
// 后台服务状态
|
||||
let isInitialized = false;
|
||||
let vibrationConfig: VibrationConfig = { ...defaultVibrationConfig };
|
||||
let appStateSubscription: any = null;
|
||||
|
||||
// 定义后台任务
|
||||
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
|
||||
console.log('[BackgroundService] 后台任务执行中...');
|
||||
|
||||
try {
|
||||
// 检查 WebSocket 连接状态
|
||||
if (!websocketService.isConnected()) {
|
||||
console.log('[BackgroundService] WebSocket 断开,尝试重连');
|
||||
await websocketService.connect();
|
||||
}
|
||||
|
||||
// 返回收到新数据
|
||||
return BackgroundFetch.BackgroundFetchResult.NewData;
|
||||
} catch (error) {
|
||||
console.error('[BackgroundService] 后台任务执行失败:', error);
|
||||
return BackgroundFetch.BackgroundFetchResult.Failed;
|
||||
}
|
||||
});
|
||||
|
||||
// WebSocket 保活任务
|
||||
TaskManager.defineTask(WEBSOCKET_KEEPALIVE_TASK, async () => {
|
||||
console.log('[BackgroundService] WebSocket 保活任务执行...');
|
||||
|
||||
try {
|
||||
if (!websocketService.isConnected()) {
|
||||
console.log('[BackgroundService] WebSocket 断开,尝试重连');
|
||||
await websocketService.connect();
|
||||
}
|
||||
return BackgroundFetch.BackgroundFetchResult.NewData;
|
||||
} catch (error) {
|
||||
console.error('[BackgroundService] WebSocket 保活失败:', error);
|
||||
return BackgroundFetch.BackgroundFetchResult.Failed;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* 触发震动反馈
|
||||
* @param type 震动类型
|
||||
*/
|
||||
export async function triggerVibration(
|
||||
type: 'light' | 'medium' | 'heavy' | 'success' | 'warning' | 'error' = 'light'
|
||||
): Promise<void> {
|
||||
if (!vibrationConfig.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
switch (type) {
|
||||
case 'light':
|
||||
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
|
||||
break;
|
||||
case 'medium':
|
||||
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
|
||||
break;
|
||||
case 'heavy':
|
||||
await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy);
|
||||
break;
|
||||
case 'success':
|
||||
await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
|
||||
break;
|
||||
case 'warning':
|
||||
await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning);
|
||||
break;
|
||||
case 'error':
|
||||
await Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[BackgroundService] 震动反馈失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 收到消息时触发震动
|
||||
* @param messageType 消息类型
|
||||
*/
|
||||
export async function vibrateOnMessage(messageType: 'chat' | 'group_message' | 'notification'): Promise<void> {
|
||||
if (!vibrationConfig.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (messageType) {
|
||||
case 'chat':
|
||||
if (vibrationConfig.onChatMessage) {
|
||||
await triggerVibration('light');
|
||||
}
|
||||
break;
|
||||
case 'group_message':
|
||||
if (vibrationConfig.onGroupMessage) {
|
||||
await triggerVibration('light');
|
||||
}
|
||||
break;
|
||||
case 'notification':
|
||||
if (vibrationConfig.onNotification) {
|
||||
await triggerVibration('medium');
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置震动设置
|
||||
*/
|
||||
export function setVibrationConfig(config: Partial<VibrationConfig>): void {
|
||||
vibrationConfig = { ...vibrationConfig, ...config };
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前震动配置
|
||||
*/
|
||||
export function getVibrationConfig(): VibrationConfig {
|
||||
return { ...vibrationConfig };
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用/禁用震动
|
||||
*/
|
||||
export function setVibrationEnabled(enabled: boolean): void {
|
||||
vibrationConfig.enabled = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册后台任务
|
||||
*/
|
||||
async function registerBackgroundTasks(): Promise<void> {
|
||||
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, // 设备启动后自动运行
|
||||
});
|
||||
console.log('[BackgroundService] 后台任务注册成功');
|
||||
}
|
||||
|
||||
// 注册 WebSocket 保活任务
|
||||
const isWsKeepaliveRegistered = await TaskManager.isTaskRegisteredAsync(WEBSOCKET_KEEPALIVE_TASK);
|
||||
if (!isWsKeepaliveRegistered) {
|
||||
await BackgroundFetch.registerTaskAsync(WEBSOCKET_KEEPALIVE_TASK, {
|
||||
minimumInterval: 60, // 1 分钟检查一次
|
||||
stopOnTerminate: false,
|
||||
startOnBoot: true,
|
||||
});
|
||||
console.log('[BackgroundService] WebSocket 保活任务注册成功');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[BackgroundService] 注册后台任务失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消后台任务
|
||||
*/
|
||||
async function unregisterBackgroundTasks(): Promise<void> {
|
||||
try {
|
||||
await BackgroundFetch.unregisterTaskAsync(BACKGROUND_FETCH_TASK);
|
||||
await BackgroundFetch.unregisterTaskAsync(WEBSOCKET_KEEPALIVE_TASK);
|
||||
console.log('[BackgroundService] 后台任务已取消');
|
||||
} catch (error) {
|
||||
console.error('[BackgroundService] 取消后台任务失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 App 状态监听
|
||||
*/
|
||||
function setupAppStateListener(): void {
|
||||
if (appStateSubscription) {
|
||||
return;
|
||||
}
|
||||
|
||||
appStateSubscription = AppState.addEventListener('change', (nextAppState: AppStateStatus) => {
|
||||
console.log('[BackgroundService] App 状态变化:', nextAppState);
|
||||
|
||||
if (nextAppState === 'active') {
|
||||
// App 回到前台,确保连接
|
||||
console.log('[BackgroundService] App 回到前台');
|
||||
if (!websocketService.isConnected()) {
|
||||
websocketService.connect();
|
||||
}
|
||||
} else if (nextAppState === 'background') {
|
||||
// App 进入后台,启动保活
|
||||
console.log('[BackgroundService] App 进入后台,启动保活机制');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化后台保活服务
|
||||
*/
|
||||
export async function initBackgroundService(): Promise<boolean> {
|
||||
if (isInitialized) {
|
||||
console.log('[BackgroundService] 服务已初始化');
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('[BackgroundService] 初始化后台保活服务...');
|
||||
|
||||
// 检查后台任务状态
|
||||
const status = await BackgroundFetch.getStatusAsync();
|
||||
if (status !== BackgroundFetch.BackgroundFetchStatus.Available) {
|
||||
console.warn('[BackgroundService] 后台任务不可用,状态:', status);
|
||||
// 即使后台任务不可用,也继续初始化其他功能
|
||||
}
|
||||
|
||||
// 注册后台任务
|
||||
await registerBackgroundTasks();
|
||||
|
||||
// 设置 App 状态监听
|
||||
setupAppStateListener();
|
||||
|
||||
isInitialized = true;
|
||||
console.log('[BackgroundService] 后台保活服务初始化完成');
|
||||
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;
|
||||
console.log('[BackgroundService] 后台保活服务已停止');
|
||||
} catch (error) {
|
||||
console.error('[BackgroundService] 停止服务失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查后台服务状态
|
||||
*/
|
||||
export async function checkBackgroundStatus(): Promise<{
|
||||
isAvailable: boolean;
|
||||
isInitialized: boolean;
|
||||
registeredTasks: string[];
|
||||
}> {
|
||||
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;
|
||||
Reference in New Issue
Block a user