From bc86dc4a0d649c402046bb2d1e626f3f1bcf82f2 Mon Sep 17 00:00:00 2001 From: AIIrondev Date: Wed, 29 Jul 2026 11:43:18 +0200 Subject: [PATCH] Fix of the notification subscription --- Web/app.py | 8 +++++--- Web/push_notifications.py | 23 +++++++++++++++-------- Web/requirements.txt | 3 ++- requirements.txt | 3 ++- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/Web/app.py b/Web/app.py index 836c29b..6b53ebb 100755 --- a/Web/app.py +++ b/Web/app.py @@ -10417,15 +10417,17 @@ def notifications_view(): admin_notifications = [] for notif in notifications: is_read = username in (notif.get('ReadBy') or []) + created_at_val = notif.get('CreatedAt') + row = { 'id': str(notif.get('_id')), 'title': notif.get('Title', 'Benachrichtigung'), 'message': notif.get('Message', ''), 'severity': notif.get('Severity', 'info'), - 'type': notif.get('Type', ''), - 'created_at': notif.get('CreatedAt'), + 'type': notif.get('Type', '') + 'created_at': created_at_val if created_at_val else None, 'is_read': is_read, - 'reference': notif.get('Reference', {}) or {}, + 'reference': notif.get('Reference') or {}, } if notif.get('Audience') == 'admin': admin_notifications.append(row) diff --git a/Web/push_notifications.py b/Web/push_notifications.py index 5c28433..58b9fcf 100644 --- a/Web/push_notifications.py +++ b/Web/push_notifications.py @@ -308,8 +308,8 @@ def _send_fcm_notification(subscription, payload): def _send_web_push_notification(subscription, payload): try: - from pywebpush import webpush - + from pywebpush import webpush, WebPushException + webpush( subscription_info={ 'endpoint': subscription['Endpoint'], @@ -319,18 +319,25 @@ def _send_web_push_notification(subscription, payload): vapid_private_key=VAPID_PRIVATE_KEY, vapid_claims={'sub': VAPID_SUBJECT}, timeout=10, - ttl=3600 + ttl=3600 ) - return True - + except ImportError: logger.warning('pywebpush not installed. pip install pywebpush') return False - except Exception as e: - logger.error(f'Web push error: {e}') - return False + # HÄRTUNG: Spezifische WebPush-Fehler abfangen + except WebPushException as ex: + # HTTP 404 (Not Found) oder 410 (Gone) bedeuten: Abo existiert nicht mehr + if ex.response is not None and ex.response.status_code in [404, 410]: + logger.info(f"Subscription expired or revoked (Code {ex.response.status_code}). Marking inactive.") + return False # False signalisiert der übergeordneten Funktion, das Abo zu deaktivieren + logger.error(f'Web push failed with code {ex.response.status_code if ex.response else "Unknown"}: {ex}') + return False + except Exception as e: + logger.error(f'Unexpected Web push error: {e}') + return False def _mark_subscription_inactive(subscription_id): try: diff --git a/Web/requirements.txt b/Web/requirements.txt index b817b46..5c7a4e2 100755 --- a/Web/requirements.txt +++ b/Web/requirements.txt @@ -16,4 +16,5 @@ openpyxl cryptography>=42.0.0 pywebpush py-vapid>=1.9.0 -beautifulsoup4 \ No newline at end of file +beautifulsoup4 +pywebpush \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index b6aa36a..e30870e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,4 +16,5 @@ openpyxl cryptography>=42.0.0 pywebpush py-vapid>=1.9.0 -beautifulsoup4 \ No newline at end of file +beautifulsoup4 +pywebpush \ No newline at end of file