Skip to content

Commit b59fbfb

Browse files
Add Support for mutable_content in aioapns (#755)
* Add Support for mutable_content in aioapns * Add test for mutable_content in apns_async * Fix type hinting and add docstring * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 29d7ad4 commit b59fbfb

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

push_notifications/apns_async.py

+16
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def apns_send_message(
222222
loc_key: str = None,
223223
priority: int = None,
224224
collapse_id: str = None,
225+
mutable_content: bool = False,
225226
err_func: ErrFunc = None,
226227
):
227228
"""
@@ -238,6 +239,9 @@ def apns_send_message(
238239
:param alert: The alert message to send
239240
:param application_id: The application_id to use
240241
:param creds: The credentials to use
242+
:param mutable_content: If True, enables the "mutable-content" flag in the payload.
243+
This allows the app's Notification Service Extension to modify
244+
the notification before it is displayed.
241245
"""
242246
results = apns_send_bulk_message(
243247
registration_ids=[registration_id],
@@ -253,6 +257,7 @@ def apns_send_message(
253257
loc_key=loc_key,
254258
priority=priority,
255259
collapse_id=collapse_id,
260+
mutable_content=mutable_content,
256261
err_func=err_func,
257262
)
258263

@@ -277,6 +282,7 @@ def apns_send_bulk_message(
277282
loc_key: str = None,
278283
priority: int = None,
279284
collapse_id: str = None,
285+
mutable_content: bool = False,
280286
err_func: ErrFunc = None,
281287
):
282288
"""
@@ -291,6 +297,9 @@ def apns_send_bulk_message(
291297
:param alert: The alert message to send
292298
:param application_id: The application_id to use
293299
:param creds: The credentials to use
300+
:param mutable_content: If True, enables the "mutable-content" flag in the payload.
301+
This allows the app's Notification Service Extension to modify
302+
the notification before it is displayed.
294303
"""
295304
try:
296305
topic = get_manager().get_apns_topic(application_id)
@@ -311,6 +320,7 @@ def apns_send_bulk_message(
311320
loc_key=loc_key,
312321
priority=priority,
313322
collapse_id=collapse_id,
323+
mutable_content=mutable_content,
314324
err_func=err_func,
315325
))
316326

@@ -355,12 +365,17 @@ async def _send_bulk_request(
355365
loc_key: str = None,
356366
priority: int = None,
357367
collapse_id: str = None,
368+
mutable_content: bool = False,
358369
err_func: ErrFunc = None,
359370
):
360371
client = _create_client(
361372
creds=creds, application_id=application_id, topic=topic, err_func=err_func
362373
)
363374

375+
aps_kwargs = {}
376+
if mutable_content:
377+
aps_kwargs["mutable-content"] = mutable_content
378+
364379
requests = [_create_notification_request_from_args(
365380
registration_id,
366381
alert,
@@ -372,6 +387,7 @@ async def _send_bulk_request(
372387
loc_key=loc_key,
373388
priority=priority,
374389
collapse_id=collapse_id,
390+
aps_kwargs=aps_kwargs
375391
) for registration_id in registration_ids]
376392

377393
send_requests = [_send_request(client, request) for request in requests]

tests/test_apns_async_push_payload.py

+19
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,25 @@ def test_aioapns_err_func(self, mock_cert_pool):
181181
mock.ANY, result
182182
)
183183

184+
@mock.patch("push_notifications.apns_async.APNs", autospec=True)
185+
def test_push_payload_with_mutable_content(self, mock_apns):
186+
apns_send_message(
187+
"123",
188+
"Hello world",
189+
mutable_content=True,
190+
creds=TokenCredentials(key="aaa", key_id="bbb", team_id="ccc"),
191+
sound="chime",
192+
extra={"custom_data": 12345},
193+
expiration=int(time.time()) + 3,
194+
)
195+
196+
args, kwargs = mock_apns.return_value.send_notification.call_args
197+
req = args[0]
198+
199+
# Assertions
200+
self.assertTrue("mutable-content" in req.message["aps"])
201+
self.assertEqual(req.message["aps"]["mutable-content"], 1) # APNs expects 1 for True
202+
184203
# def test_bad_priority(self):
185204
# with mock.patch("apns2.credentials.init_context"):
186205
# with mock.patch("apns2.client.APNsClient.connect"):

0 commit comments

Comments
 (0)