feat(src): implement Web Push notifications infrastructure including VAPID key management, subscription endpoints, and push notification dispatch
Added context processor, management command, and supporting models/views to enable Web Push notifications and VAPID key generation.
This commit is contained in:
9 files changed
+192
-3
No files matched your search
@@ -0,0 +1,55 @@
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from pywebpush import WebPushException, webpush
|
||||
|
||||
from src.notifications.models import PushSubscription
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _get_vapid_private_key():
|
||||
raw = settings.VAPID_PRIVATE_KEY
|
||||
if not raw:
|
||||
return None
|
||||
return raw.replace("\\n", "\n")
|
||||
|
||||
|
||||
def send_push_notification(user, title, body, url=None, icon=None):
|
||||
private_key = _get_vapid_private_key()
|
||||
if not settings.VAPID_PUBLIC_KEY or not private_key:
|
||||
logger.warning("VAPID keys not configured; skipping push notification")
|
||||
return
|
||||
|
||||
payload = {
|
||||
"title": title,
|
||||
"body": body,
|
||||
"icon": icon or "/static/img/favicon.png",
|
||||
"badge": "/static/img/favicon.png",
|
||||
}
|
||||
if url:
|
||||
payload["data"] = {"url": url}
|
||||
|
||||
subscriptions = PushSubscription.objects.filter(user=user)
|
||||
if not subscriptions.exists():
|
||||
return
|
||||
|
||||
for sub in subscriptions:
|
||||
try:
|
||||
webpush(
|
||||
subscription_info={
|
||||
"endpoint": sub.endpoint,
|
||||
"keys": {"auth": sub.auth_key, "p256dh": sub.p256dh_key},
|
||||
},
|
||||
data=payload,
|
||||
vapid_private_key=private_key,
|
||||
vapid_claims=settings.VAPID_CLAIMS,
|
||||
)
|
||||
except WebPushException as e:
|
||||
if e.response and e.response.status_code in (410, 404):
|
||||
logger.info("Removing expired push subscription for %s", user)
|
||||
sub.delete()
|
||||
else:
|
||||
logger.error("Web push failed for %s: %s", user, e)
|
||||
except Exception as e:
|
||||
logger.error("Unexpected error sending push to %s: %s", user, e)
|
||||
Reference in new issue
Block a user