Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Urgent Notif (iOS) #333

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 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))

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

View check run for this annotation

Codecov / codecov/patch

backend/user/notifications.py#L31

Added line #L31 was not covered by tests

def send_shadow_notification(self, tokens, body):
self.send_payload(tokens, self.create_shadow_payload(body))
Expand All @@ -42,7 +42,7 @@
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 @@
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,18 @@


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

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

View check run for this annotation

Codecov / codecov/patch

backend/user/notifications.py#L91-L92

Added lines #L91 - L92 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

backend/user/notifications.py#L95-L98

Added lines #L95 - L98 were not covered by tests

@staticmethod
def get_client(is_dev):
auth_key_path = (
Expand All @@ -98,10 +111,14 @@
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(

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

View check run for this annotation

Codecov / codecov/patch

backend/user/notifications.py#L116

Added line #L116 was not covered by tests
alert={"title": title, "body": body},
sound="default",
badge=0,
mutable_content=True,
urgent=urgent,
)

def create_shadow_payload(self, body):
Expand All @@ -121,7 +138,7 @@


@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 +148,7 @@


@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 +158,7 @@


@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 @@
title = request.data.get("title")
body = request.data.get("body")
delay = max(request.data.get("delay", 0), 0)
urgent = request.data.get("urgent", False)

Check warning on line 142 in backend/user/views.py

View check run for this annotation

Codecov / codecov/patch

backend/user/views.py#L142

Added line #L142 was not covered by tests

if None in [service, title, body]:
return Response({"detail": "Missing required parameters."}, status=400)
Expand All @@ -160,7 +161,7 @@
(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)

Check warning on line 164 in backend/user/views.py

View check run for this annotation

Codecov / codecov/patch

backend/user/views.py#L164

Added line #L164 was not covered by tests

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
Loading