@@ -391,13 +391,14 @@ def show_notification(
391391 raise ValueError (f"不支持的通知类型: { notification_type } " )
392392
393393
394- def send_system_notification (title : str , content : str ) -> bool :
394+ def send_system_notification (title : str , content : str , url : str = None ) -> bool :
395395 """
396396 发送系统通知
397397
398398 Args:
399399 title: 通知标题
400400 content: 通知内容
401+ url: 点击通知后跳转的URL,默认为下载链接
401402
402403 Returns:
403404 bool: 通知发送是否成功
@@ -406,6 +407,20 @@ def send_system_notification(title: str, content: str) -> bool:
406407 # 获取软件图标路径
407408 icon_path = str (get_data_path ("assets" , "icon/secrandom-icon-paper.ico" ))
408409
410+ # 定义点击通知的回调函数
411+ def on_notification_click ():
412+ """点击通知时执行的函数"""
413+ try :
414+ if url :
415+ import webbrowser
416+
417+ webbrowser .open (url )
418+ logger .debug (f"已打开通知链接: { url } " )
419+ else :
420+ logger .warning ("通知未配置URL,无法打开链接" )
421+ except Exception as e :
422+ logger .error (f"打开通知链接失败: { e } " )
423+
409424 if sys .platform == "win32" :
410425 # Windows平台
411426 try :
@@ -414,7 +429,12 @@ def send_system_notification(title: str, content: str) -> bool:
414429
415430 toaster = ToastNotifier ()
416431 toaster .show_toast (
417- title , content , icon_path = icon_path , duration = 15 , threaded = True
432+ title ,
433+ content ,
434+ icon_path = icon_path ,
435+ duration = 0 ,
436+ threaded = True ,
437+ callback_on_click = on_notification_click ,
418438 )
419439 logger .debug (f"已发送Windows通知: { title } " )
420440 return True
@@ -428,7 +448,7 @@ def send_system_notification(title: str, content: str) -> bool:
428448 message = content ,
429449 app_name = APPLY_NAME ,
430450 app_icon = icon_path ,
431- timeout = 15 ,
451+ timeout = 0 ,
432452 )
433453 logger .debug (f"已发送Windows通知(使用plyer): { title } " )
434454 return True
@@ -441,12 +461,28 @@ def send_system_notification(title: str, content: str) -> bool:
441461 # 尝试使用notify-send命令
442462 import subprocess
443463
444- subprocess .run (
445- ["notify-send" , "--icon" , icon_path , title , content ],
446- check = True ,
447- timeout = 5 ,
448- )
449- logger .debug (f"已发送Linux通知: { title } " )
464+ if url :
465+ subprocess .run (
466+ [
467+ "notify-send" ,
468+ "--icon" ,
469+ icon_path ,
470+ "--action" ,
471+ f"default={ url } " ,
472+ title ,
473+ content ,
474+ ],
475+ check = True ,
476+ timeout = 0 ,
477+ )
478+ logger .debug (f"已发送Linux通知(包含URL): { title } " )
479+ else :
480+ subprocess .run (
481+ ["notify-send" , "--icon" , icon_path , title , content ],
482+ check = True ,
483+ timeout = 0 ,
484+ )
485+ logger .debug (f"已发送Linux通知(不包含URL): { title } " )
450486 return True
451487 except subprocess .CalledProcessError as e :
452488 # 如果notify-send不可用,尝试使用plyer
@@ -458,7 +494,7 @@ def send_system_notification(title: str, content: str) -> bool:
458494 message = content ,
459495 app_name = APPLY_NAME ,
460496 app_icon = icon_path ,
461- timeout = 15 ,
497+ timeout = 0 ,
462498 )
463499 logger .debug (f"已发送Linux通知(使用plyer): { title } " )
464500 return True
0 commit comments