diff --git a/app.py b/app.py index 0e5df86..2df65cb 100644 --- a/app.py +++ b/app.py @@ -132,21 +132,30 @@ class FlicButtonHandler: logger.error(f"Error saving subscriptions: {e}") 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: if not self.subscriptions: logger.warning("No subscriptions available") return + # Debug output + logger.debug(f"Using VAPID key: {self.vapid_private_key[:50]}...") + logger.debug(f"Subscription endpoint: {subscription['endpoint'][:50]}...") + webpush( subscription_info=subscription, data=message, 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: - logger.error(f"Push notification error: {e}") - # Remove invalid subscription + logger.error(f"Push notification error: {str(e)}") + 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.save_subscriptions() @@ -169,16 +178,20 @@ class FlicButtonHandler: await self.broadcast_notification(message) async def broadcast_notification(self, message: str): - """Broadcast notification to all subscriptions.""" + """Broadcast notification to all subscriptions with error handling.""" if not self.subscriptions: logger.warning("No subscriptions to broadcast to") return - tasks = [ - self.send_push_notification(subscription, message) - for subscription in self.subscriptions - ] - await asyncio.gather(*tasks) + success_count = 0 + for subscription in self.subscriptions: + try: + await self.send_push_notification(subscription, message) + 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): """Webhook endpoint for Flic button events."""