From 4a904a11a8e69a744b07fe2ae9e64ff2f6958eec Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Sat, 4 Oct 2025 12:25:41 -0700 Subject: remove fcm tokens from kv when they are unresponsive --- notifications.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'notifications.py') diff --git a/notifications.py b/notifications.py index bb235fa..0ec36cb 100644 --- a/notifications.py +++ b/notifications.py @@ -55,8 +55,21 @@ def broadcast_to_topic(topic: str, title: str, body: str, image): print(f"Successfully sent {response.success_count} messages out of {len(tokens)}") if response.failure_count > 0: print(f"Failed to send {response.failure_count} messages") + upstash_rest_api_url = os.environ.get("KV_REST_API_URL") + upstash_token = os.environ.get("KV_REST_API_TOKEN") for idx, error in enumerate(response.responses): if not error.success: - print(f"Error for token {tokens[idx]}: {error.exception}") + failed_token = tokens[idx] + print(f"Error for token {failed_token}: {error.exception}") + url = f"{upstash_rest_api_url}/srem/fcm-{topic}" + resp = requests.post( + url, + headers={"Authorization": f"Bearer {upstash_token}"}, + json={"value": failed_token} + ) + if resp.status_code != 200: + print(f"Failed to remove token {failed_token} from topic '{topic}': {resp.text}") + else: + print(f"Removed token {failed_token} from topic '{topic}'") except Exception as e: print(f"Error sending multicast message: {e}") -- cgit v1.2.3