This commit is contained in:
2026-01-13 15:58:04 +08:00
parent 45d5debead
commit 257f1cae69
11 changed files with 1079 additions and 23 deletions

View File

@@ -0,0 +1,84 @@
/**
* 推送通知 Hook
* 在 App 中初始化和处理推送通知
*/
import { useEffect, useRef, useCallback } from 'react';
import { useNavigation } from '@react-navigation/native';
import { pushService } from '../services/pushService';
/**
* 推送通知 Hook
* @param {object} options
* @param {boolean} options.enabled - 是否启用推送
* @returns {object} { deviceToken, clearBadge }
*/
export function usePushNotifications(options = { enabled: true }) {
const navigation = useNavigation();
const deviceTokenRef = useRef(null);
// 处理收到的通知App 在前台时)
const handleNotificationReceived = useCallback((notification) => {
const data = notification.request.content.data;
console.log('[Push Hook] 前台收到通知:', data);
// 可以在这里触发列表刷新等操作
// 例如dispatch(fetchEvents({ refresh: true }));
}, []);
// 处理用户点击通知
const handleNotificationResponse = useCallback((response) => {
const data = response.notification.request.content.data;
console.log('[Push Hook] 用户点击通知:', data);
// 根据通知数据导航到对应页面
if (data?.event_id) {
// 导航到事件详情
navigation.navigate('EventsDrawer', {
screen: 'EventDetail',
params: {
eventId: data.event_id,
title: data.title || '事件详情',
},
});
}
}, [navigation]);
// 初始化推送服务
useEffect(() => {
if (!options.enabled) return;
const initPush = async () => {
try {
const token = await pushService.initialize(
handleNotificationReceived,
handleNotificationResponse
);
deviceTokenRef.current = token;
console.log('[Push Hook] 推送初始化完成, token:', token ? '已获取' : '未获取');
} catch (error) {
console.error('[Push Hook] 推送初始化失败:', error);
}
};
initPush();
// 清理
return () => {
// 注意:不在这里 unregister因为我们希望保持推送注册
// 如果需要完全退出登录时取消注册,应该在 logout 时调用 pushService.unregister()
};
}, [options.enabled, handleNotificationReceived, handleNotificationResponse]);
// 清除角标
const clearBadge = useCallback(() => {
pushService.clearBadge();
}, []);
return {
deviceToken: deviceTokenRef.current,
clearBadge,
};
}
export default usePushNotifications;