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 Support for mutable_content in aioapns #755

Merged
merged 4 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions push_notifications/apns_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def apns_send_message(
loc_key: str = None,
priority: int = None,
collapse_id: str = None,
mutable_content: int = None,
err_func: ErrFunc = None,
):
"""
Expand Down Expand Up @@ -253,6 +254,7 @@ def apns_send_message(
loc_key=loc_key,
priority=priority,
collapse_id=collapse_id,
mutable_content=mutable_content,
err_func=err_func,
)

Expand All @@ -277,6 +279,7 @@ def apns_send_bulk_message(
loc_key: str = None,
priority: int = None,
collapse_id: str = None,
mutable_content: int = None,
err_func: ErrFunc = None,
):
"""
Expand Down Expand Up @@ -311,6 +314,7 @@ def apns_send_bulk_message(
loc_key=loc_key,
priority=priority,
collapse_id=collapse_id,
mutable_content=mutable_content,
err_func=err_func,
))

Expand Down Expand Up @@ -355,12 +359,17 @@ async def _send_bulk_request(
loc_key: str = None,
priority: int = None,
collapse_id: str = None,
mutable_content: str = None,
err_func: ErrFunc = None,
):
client = _create_client(
creds=creds, application_id=application_id, topic=topic, err_func=err_func
)

aps_kwargs = {}
if mutable_content:
aps_kwargs["mutable-content"] = mutable_content

requests = [_create_notification_request_from_args(
registration_id,
alert,
Expand All @@ -372,6 +381,7 @@ async def _send_bulk_request(
loc_key=loc_key,
priority=priority,
collapse_id=collapse_id,
aps_kwargs=aps_kwargs
) for registration_id in registration_ids]

send_requests = [_send_request(client, request) for request in requests]
Expand Down
19 changes: 19 additions & 0 deletions tests/test_apns_async_push_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,25 @@ def test_aioapns_err_func(self, mock_cert_pool):
mock.ANY, result
)

@mock.patch("push_notifications.apns_async.APNs", autospec=True)
def test_push_payload_with_mutable_content(self, mock_apns):
apns_send_message(
"123",
"Hello world",
mutable_content=True,
creds=TokenCredentials(key="aaa", key_id="bbb", team_id="ccc"),
sound="chime",
extra={"custom_data": 12345},
expiration=int(time.time()) + 3,
)

args, kwargs = mock_apns.return_value.send_notification.call_args
req = args[0]

# Assertions
self.assertTrue("mutable-content" in req.message["aps"])
self.assertEqual(req.message["aps"]["mutable-content"], 1) # APNs expects 1 for True

# def test_bad_priority(self):
# with mock.patch("apns2.credentials.init_context"):
# with mock.patch("apns2.client.APNsClient.connect"):
Expand Down
Loading