diff --git a/routes/api.py b/routes/api.py index ecd4553..3af7415 100644 --- a/routes/api.py +++ b/routes/api.py @@ -227,16 +227,29 @@ def send_test_notification_route(username: str, password: str) -> Response: data = request.get_json() message = data.get("message", "Ktv Test") if data else "Ktv Test" - subscriptions = get_push_subscriptions() # Get all subscriptions + try: + subscriptions = get_push_subscriptions() # Get all subscriptions + except Exception as e: + print(f"Error getting push subscriptions: {e}") + return jsonify({"error": "Could not retrieve push subscriptions from the database."}), 500 + if not subscriptions: return jsonify({"message": "No push subscriptions found."}), 404 message_body = json.dumps({"title": "KTVManager", "body": message}) + success_count = 0 + failure_count = 0 for sub in subscriptions: try: send_notification(json.loads(sub['subscription_json']), message_body) + success_count += 1 except Exception as e: print(f"Error sending notification to subscription ID {sub.get('id', 'N/A')}: {e}") + failure_count += 1 - return jsonify({"message": f"Test notification sent to {len(subscriptions)} subscription(s)."}) \ No newline at end of file + return jsonify({ + "message": f"Test notification sending process completed.", + "sent": success_count, + "failed": failure_count + }) \ No newline at end of file