fix webpush

This commit is contained in:
jxxghp
2024-06-07 21:53:48 +08:00
parent 9e0751367b
commit f8fbf9b5eb
2 changed files with 8 additions and 8 deletions

View File

@ -1,7 +1,7 @@
import json
from typing import Union, Any, List
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
from fastapi import APIRouter, BackgroundTasks, Depends
from fastapi import Request
from pywebpush import WebPushException, webpush
from sqlalchemy.orm import Session
@ -174,8 +174,8 @@ def send_notification(payload: schemas.SubscriptionMessage, _: schemas.TokenPayl
"""
发送webpush通知
"""
try:
for sub in global_vars.get_subscriptions():
try:
webpush(
subscription_info=sub,
data=json.dumps(payload.dict()),
@ -184,7 +184,7 @@ def send_notification(payload: schemas.SubscriptionMessage, _: schemas.TokenPayl
"sub": settings.VAPID.get("subject")
},
)
except WebPushException as err:
logger.error(f"WebPush发送失败: {str(err)}")
continue
return schemas.Response(success=True)
except WebPushException as ex:
print("WebPush Error:", repr(ex))
raise HTTPException(status_code=500, detail=str(ex))

View File

@ -61,7 +61,7 @@ class WebPushModule(_ModuleBase):
},
)
except WebPushException as err:
print("WebPush Error:", str(err))
logger.error(f"WebPush发送失败: {str(err)}")
except Exception as msg_e:
logger.error(f"发送消息失败:{msg_e}")