|
| 1 | +import { |
| 2 | + DesktopNotificationResult, |
| 3 | + ShowDesktopNotificationParams, |
| 4 | +} from '@lobechat/electron-client-ipc'; |
| 5 | +import { Notification, app } from 'electron'; |
| 6 | +import { macOS, windows } from 'electron-is'; |
| 7 | + |
| 8 | +import { createLogger } from '@/utils/logger'; |
| 9 | + |
| 10 | +import { ControllerModule, ipcClientEvent } from './index'; |
| 11 | + |
| 12 | +const logger = createLogger('controllers:NotificationCtr'); |
| 13 | + |
| 14 | +export default class NotificationCtr extends ControllerModule { |
| 15 | + /** |
| 16 | + * 在应用准备就绪后设置桌面通知 |
| 17 | + */ |
| 18 | + afterAppReady() { |
| 19 | + this.setupNotifications(); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * 设置桌面通知权限和配置 |
| 24 | + */ |
| 25 | + private setupNotifications() { |
| 26 | + logger.debug('Setting up desktop notifications'); |
| 27 | + |
| 28 | + try { |
| 29 | + // 检查通知支持 |
| 30 | + if (!Notification.isSupported()) { |
| 31 | + logger.warn('Desktop notifications are not supported on this platform'); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + // 在 macOS 上,我们可能需要显式请求通知权限 |
| 36 | + if (macOS()) { |
| 37 | + logger.debug('macOS detected, notification permissions should be handled by system'); |
| 38 | + } |
| 39 | + |
| 40 | + // 在 Windows 上设置应用用户模型 ID |
| 41 | + if (windows()) { |
| 42 | + app.setAppUserModelId('com.lobehub.chat'); |
| 43 | + logger.debug('Set Windows App User Model ID for notifications'); |
| 44 | + } |
| 45 | + |
| 46 | + logger.info('Desktop notifications setup completed'); |
| 47 | + } catch (error) { |
| 48 | + logger.error('Failed to setup desktop notifications:', error); |
| 49 | + } |
| 50 | + } |
| 51 | + /** |
| 52 | + * 显示系统桌面通知(仅当窗口隐藏时) |
| 53 | + */ |
| 54 | + @ipcClientEvent('showDesktopNotification') |
| 55 | + async showDesktopNotification( |
| 56 | + params: ShowDesktopNotificationParams, |
| 57 | + ): Promise<DesktopNotificationResult> { |
| 58 | + logger.debug('收到桌面通知请求:', params); |
| 59 | + |
| 60 | + try { |
| 61 | + // 检查通知支持 |
| 62 | + if (!Notification.isSupported()) { |
| 63 | + logger.warn('系统不支持桌面通知'); |
| 64 | + return { error: 'Desktop notifications not supported', success: false }; |
| 65 | + } |
| 66 | + |
| 67 | + // 检查窗口是否隐藏 |
| 68 | + const isWindowHidden = this.isMainWindowHidden(); |
| 69 | + |
| 70 | + if (!isWindowHidden) { |
| 71 | + logger.debug('主窗口可见,跳过桌面通知'); |
| 72 | + return { reason: 'Window is visible', skipped: true, success: true }; |
| 73 | + } |
| 74 | + |
| 75 | + logger.info('窗口已隐藏,显示桌面通知:', params.title); |
| 76 | + |
| 77 | + const notification = new Notification({ |
| 78 | + body: params.body, |
| 79 | + // 添加更多配置以确保通知能正常显示 |
| 80 | +hasReply: false, |
| 81 | + |
| 82 | +silent: params.silent || false, |
| 83 | + |
| 84 | + timeoutType: 'default', |
| 85 | + title: params.title, |
| 86 | + urgency: 'normal', |
| 87 | + }); |
| 88 | + |
| 89 | + // 添加更多事件监听来调试 |
| 90 | + notification.on('show', () => { |
| 91 | + logger.info('通知已显示'); |
| 92 | + }); |
| 93 | + |
| 94 | + notification.on('click', () => { |
| 95 | + logger.debug('用户点击通知,显示主窗口'); |
| 96 | + const mainWindow = this.app.browserManager.getMainWindow(); |
| 97 | + mainWindow.show(); |
| 98 | + mainWindow.browserWindow.focus(); |
| 99 | + }); |
| 100 | + |
| 101 | + notification.on('close', () => { |
| 102 | + logger.debug('通知已关闭'); |
| 103 | + }); |
| 104 | + |
| 105 | + notification.on('failed', (error) => { |
| 106 | + logger.error('通知显示失败:', error); |
| 107 | + }); |
| 108 | + |
| 109 | + // 使用 Promise 来确保通知显示 |
| 110 | + return new Promise((resolve) => { |
| 111 | + notification.show(); |
| 112 | + |
| 113 | + // 给通知一些时间来显示,然后检查结果 |
| 114 | + setTimeout(() => { |
| 115 | + logger.info('通知显示调用完成'); |
| 116 | + resolve({ success: true }); |
| 117 | + }, 100); |
| 118 | + }); |
| 119 | + } catch (error) { |
| 120 | + logger.error('显示桌面通知失败:', error); |
| 121 | + return { |
| 122 | + error: error instanceof Error ? error.message : 'Unknown error', |
| 123 | + success: false, |
| 124 | + }; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * 检查主窗口是否隐藏 |
| 130 | + */ |
| 131 | + @ipcClientEvent('isMainWindowHidden') |
| 132 | + isMainWindowHidden(): boolean { |
| 133 | + try { |
| 134 | + const mainWindow = this.app.browserManager.getMainWindow(); |
| 135 | + const browserWindow = mainWindow.browserWindow; |
| 136 | + |
| 137 | + // 如果窗口被销毁,认为是隐藏的 |
| 138 | + if (browserWindow.isDestroyed()) { |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + // 检查窗口是否可见和聚焦 |
| 143 | + const isVisible = browserWindow.isVisible(); |
| 144 | + const isFocused = browserWindow.isFocused(); |
| 145 | + const isMinimized = browserWindow.isMinimized(); |
| 146 | + |
| 147 | + logger.debug('窗口状态检查:', { isFocused, isMinimized, isVisible }); |
| 148 | + |
| 149 | + // 窗口隐藏的条件:不可见或最小化或失去焦点 |
| 150 | + return !isVisible || isMinimized || !isFocused; |
| 151 | + } catch (error) { |
| 152 | + logger.error('检查窗口状态失败:', error); |
| 153 | + return true; // 发生错误时认为窗口隐藏,确保通知能显示 |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments