diff options
| author | Pinapelz <yukais@pinapelz.com> | 2025-10-03 17:40:25 -0700 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2025-10-03 17:40:25 -0700 |
| commit | 07a150a20ed03c14e918b081acc0c6e357857ab3 (patch) | |
| tree | 7daff7a55e8dc584fc8a33c9e9bdb8d52a56724c | |
| parent | 2c349d08bd2d6b946f36d7fe9598e22cac393006 (diff) | |
use multisend when communicating with fcm
| -rw-r--r-- | notifications.py | 39 |
1 files changed, 18 insertions, 21 deletions
diff --git a/notifications.py b/notifications.py index f5c7e77..bb235fa 100644 --- a/notifications.py +++ b/notifications.py @@ -42,24 +42,21 @@ def broadcast_to_topic(topic: str, title: str, body: str, image): print(f"No tokens found for topic '{topic}'") return - for token in tokens: - message = messaging.Message( - notification=messaging.Notification( - title=title, - body=body, - image=image - ), - token=token - ) - try: - response = messaging.send(message) - print(f"Sent to {token}: {response}") - except Exception as e: - print(f"Error sending to {token}: {e}") - -if __name__ == "__main__": - topic = "sdvx" - title = "New sdvx Update!" - body = "Check out the latest DDR content now!" - - broadcast_to_topic(topic, title, body) + multicast_message = messaging.MulticastMessage( + notification=messaging.Notification( + title=title, + body=body, + image=image + ), + tokens=tokens + ) + try: + response = messaging.send_each_for_multicast(multicast_message) + 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") + for idx, error in enumerate(response.responses): + if not error.success: + print(f"Error for token {tokens[idx]}: {error.exception}") + except Exception as e: + print(f"Error sending multicast message: {e}") |
