调整socket对应的浏览器通知处理逻辑
This commit is contained in:
@@ -56,6 +56,7 @@ import { logger } from '../../../utils/logger';
|
||||
import { getApiBase } from '../../../utils/apiConfig';
|
||||
import { useEventNotifications } from '../../../hooks/useEventNotifications';
|
||||
import { getImportanceConfig, getAllImportanceLevels } from '../../../constants/importanceLevels';
|
||||
import { browserNotificationService } from '../../../services/browserNotificationService';
|
||||
|
||||
// ========== 工具函数定义在组件外部 ==========
|
||||
// 涨跌颜色配置(中国A股配色:红涨绿跌)- 分档次显示
|
||||
@@ -136,6 +137,19 @@ const EventList = ({ events, pagination, onPageChange, onEventClick, onViewDetai
|
||||
const [followingMap, setFollowingMap] = useState({});
|
||||
const [followCountMap, setFollowCountMap] = useState({});
|
||||
const [localEvents, setLocalEvents] = useState(events); // 用于实时更新的本地事件列表
|
||||
const [notificationPermission, setNotificationPermission] = useState('default');
|
||||
|
||||
// 请求浏览器通知权限
|
||||
useEffect(() => {
|
||||
const requestPermission = async () => {
|
||||
if (browserNotificationService.isSupported()) {
|
||||
const permission = await browserNotificationService.requestPermission();
|
||||
setNotificationPermission(permission);
|
||||
logger.info('EventList', '浏览器通知权限状态', { permission });
|
||||
}
|
||||
};
|
||||
requestPermission();
|
||||
}, []);
|
||||
|
||||
// 实时事件推送集成
|
||||
const { isConnected } = useEventNotifications({
|
||||
@@ -162,6 +176,33 @@ const EventList = ({ events, pagination, onPageChange, onEventClick, onViewDetai
|
||||
});
|
||||
console.log('[EventList DEBUG] ✓ Toast 通知已调用,ID:', toastId);
|
||||
|
||||
// 发送浏览器原生通知
|
||||
console.log('[EventList DEBUG] 准备发送浏览器原生通知');
|
||||
const currentPermission = browserNotificationService.getPermissionStatus();
|
||||
console.log('[EventList DEBUG] 通知权限状态:', currentPermission);
|
||||
if (currentPermission === 'granted') {
|
||||
const importance = getImportanceConfig(event.importance);
|
||||
const notification = browserNotificationService.sendNotification({
|
||||
title: `🔔 ${importance.label}级事件`,
|
||||
body: event.title,
|
||||
tag: `event_${event.id}`,
|
||||
data: {
|
||||
link: `/event-detail/${event.id}`,
|
||||
eventId: event.id,
|
||||
},
|
||||
autoClose: 10000, // 10秒后自动关闭
|
||||
});
|
||||
|
||||
if (notification) {
|
||||
browserNotificationService.setupClickHandler(notification, navigate);
|
||||
console.log('[EventList DEBUG] ✓ 浏览器原生通知已发送');
|
||||
} else {
|
||||
console.log('[EventList DEBUG] ⚠️ 浏览器原生通知发送失败');
|
||||
}
|
||||
} else {
|
||||
console.log('[EventList DEBUG] ⚠️ 浏览器通知权限未授予,跳过原生通知');
|
||||
}
|
||||
|
||||
console.log('[EventList DEBUG] 准备更新事件列表');
|
||||
// 将新事件添加到列表顶部(防止重复)
|
||||
setLocalEvents((prevEvents) => {
|
||||
|
||||
Reference in New Issue
Block a user