This commit is contained in:
cpu
2025-03-26 08:34:51 +01:00
parent b246923283
commit f500c00896

33
app.py
View File

@@ -132,21 +132,30 @@ class FlicButtonHandler:
logger.error(f"Error saving subscriptions: {e}") logger.error(f"Error saving subscriptions: {e}")
async def send_push_notification(self, subscription: Dict, message: str): async def send_push_notification(self, subscription: Dict, message: str):
"""Send a web push notification.""" """Send a web push notification with proper key handling."""
try: try:
if not self.subscriptions: if not self.subscriptions:
logger.warning("No subscriptions available") logger.warning("No subscriptions available")
return return
# Debug output
logger.debug(f"Using VAPID key: {self.vapid_private_key[:50]}...")
logger.debug(f"Subscription endpoint: {subscription['endpoint'][:50]}...")
webpush( webpush(
subscription_info=subscription, subscription_info=subscription,
data=message, data=message,
vapid_private_key=self.vapid_private_key, vapid_private_key=self.vapid_private_key,
vapid_claims={"sub": "mailto:your-email@example.com"} vapid_claims={
"sub": os.getenv('VAPID_CLAIM_EMAIL', 'mailto:your-email@example.com'),
"aud": subscription['endpoint'].split('/send/')[0] + "/"
}
) )
logger.info("Push notification sent successfully")
except WebPushException as e: except WebPushException as e:
logger.error(f"Push notification error: {e}") logger.error(f"Push notification error: {str(e)}")
# Remove invalid subscription if 'Invalid JWT' in str(e):
logger.error("VAPID key validation failed - check key format")
self.subscriptions = [s for s in self.subscriptions if s != subscription] self.subscriptions = [s for s in self.subscriptions if s != subscription]
self.save_subscriptions() self.save_subscriptions()
@@ -169,16 +178,20 @@ class FlicButtonHandler:
await self.broadcast_notification(message) await self.broadcast_notification(message)
async def broadcast_notification(self, message: str): async def broadcast_notification(self, message: str):
"""Broadcast notification to all subscriptions.""" """Broadcast notification to all subscriptions with error handling."""
if not self.subscriptions: if not self.subscriptions:
logger.warning("No subscriptions to broadcast to") logger.warning("No subscriptions to broadcast to")
return return
tasks = [ success_count = 0
self.send_push_notification(subscription, message) for subscription in self.subscriptions:
for subscription in self.subscriptions try:
] await self.send_push_notification(subscription, message)
await asyncio.gather(*tasks) success_count += 1
except Exception as e:
logger.error(f"Failed to send to {subscription['endpoint'][:30]}...: {str(e)}")
logger.info(f"Notifications sent: {success_count}/{len(self.subscriptions)}")
async def handle_flic_webhook(self, request): async def handle_flic_webhook(self, request):
"""Webhook endpoint for Flic button events.""" """Webhook endpoint for Flic button events."""