Skip to content

Commit

Permalink
Make new client per IOS notif
Browse files Browse the repository at this point in the history
  • Loading branch information
vcai122 committed Nov 20, 2024
1 parent 97387cd commit 2cb0501
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions backend/user/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,16 @@ 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"
)
return APNsClient(credentials=auth_key_path, use_sandbox=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}")
Expand All @@ -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(

Check warning on line 131 in backend/user/notifications.py

View check run for this annotation

Codecov / codecov/patch

backend/user/notifications.py#L131

Added line #L131 was not covered by tests
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)

Check warning on line 136 in backend/user/notifications.py

View check run for this annotation

Codecov / codecov/patch

backend/user/notifications.py#L136

Added line #L136 was not covered by tests


IOSNotificationSender = IOSNotificationWrapper()
Expand Down

0 comments on commit 2cb0501

Please sign in to comment.