From 2cb050113982a935afb932389fa649d392591c15 Mon Sep 17 00:00:00 2001 From: vcai122 Date: Tue, 19 Nov 2024 19:04:14 -0500 Subject: [PATCH] Make new client per IOS notif --- backend/user/notifications.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/backend/user/notifications.py b/backend/user/notifications.py index 53859995..120d0724 100644 --- a/backend/user/notifications.py +++ b/backend/user/notifications.py @@ -99,6 +99,8 @@ def dict(self): @staticmethod def get_client(is_dev): + # TODO: We are getting a new client for each request, might be worth + # looking into how to keep the client alive. auth_key_path = ( f"/app/secrets/notifications/ios{'/dev/apns-dev' if is_dev else '/prod/apns-prod'}.pem" ) @@ -106,7 +108,7 @@ def get_client(is_dev): def __init__(self, is_dev=False): try: - self.client = self.get_client(is_dev) + self.is_dev = is_dev self.topic = "org.pennlabs.PennMobile" + (".dev" if is_dev else "") except Exception as e: print(f"Notifications Error: Failed to initialize APNs client: {e}") @@ -126,10 +128,12 @@ def create_shadow_payload(self, body): def send_many_notifications(self, tokens, payload): notifications = [Notification(token, payload) for token in tokens] - self.client.send_notification_batch(notifications=notifications, topic=self.topic) + self.get_client(self.is_dev).send_notification_batch( + notifications=notifications, topic=self.topic + ) def send_one_notification(self, token, payload): - self.client.send_notification(token, payload, self.topic) + self.get_client(self.is_dev).send_notification(token, payload, self.topic) IOSNotificationSender = IOSNotificationWrapper()