Skip to content

Commit 1983620

Browse files
authored
Add Support for category in aioapns (#760)
* Add Support for category in aioapns * Add test case for aps category
1 parent b59fbfb commit 1983620

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

push_notifications/apns_async.py

+15
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ def apns_send_message(
223223
priority: int = None,
224224
collapse_id: str = None,
225225
mutable_content: bool = False,
226+
category: str = None,
226227
err_func: ErrFunc = None,
227228
):
228229
"""
@@ -242,6 +243,10 @@ def apns_send_message(
242243
:param mutable_content: If True, enables the "mutable-content" flag in the payload.
243244
This allows the app's Notification Service Extension to modify
244245
the notification before it is displayed.
246+
:param category: The category identifier for actionable notifications.
247+
This should match a category identifier defined in the app's
248+
Notification Content Extension or UNNotificationCategory configuration.
249+
It allows the app to display custom actions with the notification.
245250
"""
246251
results = apns_send_bulk_message(
247252
registration_ids=[registration_id],
@@ -258,6 +263,7 @@ def apns_send_message(
258263
priority=priority,
259264
collapse_id=collapse_id,
260265
mutable_content=mutable_content,
266+
category = category,
261267
err_func=err_func,
262268
)
263269

@@ -283,6 +289,7 @@ def apns_send_bulk_message(
283289
priority: int = None,
284290
collapse_id: str = None,
285291
mutable_content: bool = False,
292+
category: str = None,
286293
err_func: ErrFunc = None,
287294
):
288295
"""
@@ -300,6 +307,10 @@ def apns_send_bulk_message(
300307
:param mutable_content: If True, enables the "mutable-content" flag in the payload.
301308
This allows the app's Notification Service Extension to modify
302309
the notification before it is displayed.
310+
:param category: The category identifier for actionable notifications.
311+
This should match a category identifier defined in the app's
312+
Notification Content Extension or UNNotificationCategory configuration.
313+
It allows the app to display custom actions with the notification.
303314
"""
304315
try:
305316
topic = get_manager().get_apns_topic(application_id)
@@ -321,6 +332,7 @@ def apns_send_bulk_message(
321332
priority=priority,
322333
collapse_id=collapse_id,
323334
mutable_content=mutable_content,
335+
category=category,
324336
err_func=err_func,
325337
))
326338

@@ -366,6 +378,7 @@ async def _send_bulk_request(
366378
priority: int = None,
367379
collapse_id: str = None,
368380
mutable_content: bool = False,
381+
category: str = None,
369382
err_func: ErrFunc = None,
370383
):
371384
client = _create_client(
@@ -375,6 +388,8 @@ async def _send_bulk_request(
375388
aps_kwargs = {}
376389
if mutable_content:
377390
aps_kwargs["mutable-content"] = mutable_content
391+
if category:
392+
aps_kwargs["category"] = category
378393

379394
requests = [_create_notification_request_from_args(
380395
registration_id,

tests/test_apns_async_push_payload.py

+19
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,25 @@ def test_push_payload_with_mutable_content(self, mock_apns):
200200
self.assertTrue("mutable-content" in req.message["aps"])
201201
self.assertEqual(req.message["aps"]["mutable-content"], 1) # APNs expects 1 for True
202202

203+
@mock.patch("push_notifications.apns_async.APNs", autospec=True)
204+
def test_push_payload_with_category(self, mock_apns):
205+
apns_send_message(
206+
"123",
207+
"Hello world",
208+
category="MESSAGE_CATEGORY",
209+
creds=TokenCredentials(key="aaa", key_id="bbb", team_id="ccc"),
210+
sound="chime",
211+
extra={"custom_data": 12345},
212+
expiration=int(time.time()) + 3,
213+
)
214+
215+
args, kwargs = mock_apns.return_value.send_notification.call_args
216+
req = args[0]
217+
218+
# Assertions
219+
self.assertTrue("category" in req.message["aps"])
220+
self.assertEqual(req.message["aps"]["category"], "MESSAGE_CATEGORY") # Verify correct category value
221+
203222
# def test_bad_priority(self):
204223
# with mock.patch("apns2.credentials.init_context"):
205224
# with mock.patch("apns2.client.APNsClient.connect"):

0 commit comments

Comments
 (0)