Skip to content

Commit

Permalink
Add Urgent Notif (iOS)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcai122 committed Nov 19, 2024
1 parent 73dc150 commit 1319fdb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
36 changes: 26 additions & 10 deletions backend/user/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@


class NotificationWrapper(ABC):
def send_notification(self, tokens, title, body):
self.send_payload(tokens, self.create_payload(title, body))
def send_notification(self, tokens, title, body, urgent):
self.send_payload(tokens, self.create_payload(title, body, urgent))

def send_shadow_notification(self, tokens, body):
self.send_payload(tokens, self.create_shadow_payload(body))
Expand All @@ -42,7 +42,7 @@ def send_payload(self, tokens, payload):
self.send_one_notification(tokens[0], payload)

@abstractmethod
def create_payload(self, title, body):
def create_payload(self, title, body, urgent):
raise NotImplementedError # pragma: no cover

@abstractmethod
Expand All @@ -67,7 +67,8 @@ def __init__(self):
except Exception as e:
print(f"Notifications Error: Failed to initialize Firebase client: {e}")

def create_payload(self, title, body):
def create_payload(self, title, body, urgent):
# TODO: do something with urgent
return {"notification": messaging.Notification(title=title, body=body)}

def create_shadow_payload(self, body):
Expand All @@ -84,6 +85,17 @@ def send_one_notification(self, token, payload):


class IOSNotificationWrapper(NotificationWrapper):
class CustomPayload(Payload):
# Custom payload to support interruption_level
def __init__(self, urgent, **kwargs):
super().__init__(**kwargs)
self.urgent = urgent

def dict(self):
result = super().dict()
if self.urgent:
result["aps"]["interruption-level"] = "time-sensitive"

@staticmethod
def get_client(is_dev):
auth_key_path = (
Expand All @@ -98,10 +110,14 @@ def __init__(self, is_dev=False):
except Exception as e:
print(f"Notifications Error: Failed to initialize APNs client: {e}")

def create_payload(self, title, body):
def create_payload(self, title, body, urgent):
# TODO: we might want to add category here, but there is no use on iOS side for now
return Payload(
alert={"title": title, "body": body}, sound="default", badge=0, mutable_content=True
return IOSNotificationWrapper.CustomPayload(
alert={"title": title, "body": body},
sound="default",
badge=0,
mutable_content=True,
urgent=urgent,
)

def create_shadow_payload(self, body):
Expand All @@ -121,7 +137,7 @@ def send_one_notification(self, token, payload):


@shared_task(name="notifications.ios_send_notification")
def ios_send_notification(tokens, title, body):
def ios_send_notification(tokens, title, body, urgent):
IOSNotificationSender.send_notification(tokens, title, body)


Expand All @@ -131,7 +147,7 @@ def ios_send_shadow_notification(tokens, body):


@shared_task(name="notifications.android_send_notification")
def android_send_notification(tokens, title, body):
def android_send_notification(tokens, title, body, urgent):
AndroidNotificationSender.send_notification(tokens, title, body)


Expand All @@ -141,7 +157,7 @@ def android_send_shadow_notification(tokens, body):


@shared_task(name="notifications.ios_send_dev_notification")
def ios_send_dev_notification(tokens, title, body):
def ios_send_dev_notification(tokens, title, body, urgent):
IOSNotificationDevSender.send_notification(tokens, title, body)


Expand Down
3 changes: 2 additions & 1 deletion backend/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def post(self, request):
title = request.data.get("title")
body = request.data.get("body")
delay = max(request.data.get("delay", 0), 0)
urgent = request.data.get("urgent", False)

if None in [service, title, body]:
return Response({"detail": "Missing required parameters."}, status=400)
Expand All @@ -160,7 +161,7 @@ def post(self, request):
(android_tokens, android_send_notification),
]:
if tokens_list := list(tokens.values_list("token", flat=True)):
send.apply_async(args=(tokens_list, title, body), countdown=delay)
send.apply_async(args=(tokens_list, title, body, urgent), countdown=delay)

users_with_service_usernames = users_with_service.values_list("username", flat=True)
users_not_reached_usernames = list(set(usernames) - set(users_with_service_usernames))
Expand Down

0 comments on commit 1319fdb

Please sign in to comment.