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():
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")
},
)
return schemas.Response(success=True)
except WebPushException as ex:
print("WebPush Error:", repr(ex))
raise HTTPException(status_code=500, detail=str(ex))
except WebPushException as err:
logger.error(f"WebPush发送失败: {str(err)}")
continue
return schemas.Response(success=True)