调整socket对应的浏览器通知处理逻辑

This commit is contained in:
2025-10-24 14:22:30 +08:00
parent 3f881d000b
commit 16d04a6d28
3 changed files with 65 additions and 6 deletions

View File

@@ -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) => {