@@ -141,9 +141,7 @@ def _create_notification_request_from_args(
141
141
notification_request_kwargs_out = notification_request_kwargs .copy ()
142
142
143
143
if expiration is not None :
144
- notification_request_kwargs_out ["time_to_live" ] = expiration - int (
145
- time .time ()
146
- )
144
+ notification_request_kwargs_out ["time_to_live" ] = expiration - int (time .time ())
147
145
if priority is not None :
148
146
notification_request_kwargs_out ["priority" ] = priority
149
147
@@ -216,6 +214,7 @@ def apns_send_message(
216
214
topic : str = None ,
217
215
badge : int = None ,
218
216
sound : str = None ,
217
+ content_available : bool = None ,
219
218
extra : dict = {},
220
219
expiration : int = None ,
221
220
thread_id : str = None ,
@@ -240,13 +239,14 @@ def apns_send_message(
240
239
:param alert: The alert message to send
241
240
:param application_id: The application_id to use
242
241
:param creds: The credentials to use
243
- :param mutable_content: If True, enables the "mutable-content" flag in the payload.
244
- This allows the app's Notification Service Extension to modify
245
- the notification before it is displayed.
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.
246
245
: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.
246
+ This should match a category identifier defined in the app's
247
+ Notification Content Extension or UNNotificationCategory configuration.
248
+ It allows the app to display custom actions with the notification.
249
+ :param content_available: If True the `content-available` flag will be set to 1, allowing the app to be woken up in the background
250
250
"""
251
251
results = apns_send_bulk_message (
252
252
registration_ids = [registration_id ],
@@ -256,14 +256,15 @@ def apns_send_message(
256
256
topic = topic ,
257
257
badge = badge ,
258
258
sound = sound ,
259
+ content_available = content_available ,
259
260
extra = extra ,
260
261
expiration = expiration ,
261
262
thread_id = thread_id ,
262
263
loc_key = loc_key ,
263
264
priority = priority ,
264
265
collapse_id = collapse_id ,
265
266
mutable_content = mutable_content ,
266
- category = category ,
267
+ category = category ,
267
268
err_func = err_func ,
268
269
)
269
270
@@ -282,6 +283,7 @@ def apns_send_bulk_message(
282
283
topic : str = None ,
283
284
badge : int = None ,
284
285
sound : str = None ,
286
+ content_available : bool = None ,
285
287
extra : dict = {},
286
288
expiration : int = None ,
287
289
thread_id : str = None ,
@@ -304,37 +306,41 @@ def apns_send_bulk_message(
304
306
:param alert: The alert message to send
305
307
:param application_id: The application_id to use
306
308
:param creds: The credentials to use
307
- :param mutable_content: If True, enables the "mutable-content" flag in the payload.
308
- This allows the app's Notification Service Extension to modify
309
- the notification before it is displayed.
309
+ :param mutable_content: If True, enables the "mutable-content" flag in the payload.
310
+ This allows the app's Notification Service Extension to modify
311
+ the notification before it is displayed.
310
312
: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.
313
+ This should match a category identifier defined in the app's
314
+ Notification Content Extension or UNNotificationCategory configuration.
315
+ It allows the app to display custom actions with the notification.
316
+ :param content_available: If True the `content-available` flag will be set to 1, allowing the app to be woken up in the background
314
317
"""
315
318
try :
316
319
topic = get_manager ().get_apns_topic (application_id )
317
320
results : Dict [str , str ] = {}
318
321
inactive_tokens = []
319
322
320
- responses = asyncio .run (_send_bulk_request (
321
- registration_ids = registration_ids ,
322
- alert = alert ,
323
- application_id = application_id ,
324
- creds = creds ,
325
- topic = topic ,
326
- badge = badge ,
327
- sound = sound ,
328
- extra = extra ,
329
- expiration = expiration ,
330
- thread_id = thread_id ,
331
- loc_key = loc_key ,
332
- priority = priority ,
333
- collapse_id = collapse_id ,
334
- mutable_content = mutable_content ,
335
- category = category ,
336
- err_func = err_func ,
337
- ))
323
+ responses = asyncio .run (
324
+ _send_bulk_request (
325
+ registration_ids = registration_ids ,
326
+ alert = alert ,
327
+ application_id = application_id ,
328
+ creds = creds ,
329
+ topic = topic ,
330
+ badge = badge ,
331
+ sound = sound ,
332
+ content_available = content_available ,
333
+ extra = extra ,
334
+ expiration = expiration ,
335
+ thread_id = thread_id ,
336
+ loc_key = loc_key ,
337
+ priority = priority ,
338
+ collapse_id = collapse_id ,
339
+ mutable_content = mutable_content ,
340
+ category = category ,
341
+ err_func = err_func ,
342
+ )
343
+ )
338
344
339
345
results = {}
340
346
errors = []
@@ -344,14 +350,17 @@ def apns_send_bulk_message(
344
350
)
345
351
if not result .is_successful :
346
352
errors .append (result .description )
347
- if result .description in ["Unregistered" , "BadDeviceToken" ,
348
- "DeviceTokenNotForTopic" ]:
353
+ if result .description in [
354
+ "Unregistered" ,
355
+ "BadDeviceToken" ,
356
+ "DeviceTokenNotForTopic" ,
357
+ ]:
349
358
inactive_tokens .append (registration_id )
350
359
351
360
if len (inactive_tokens ) > 0 :
352
- models .APNSDevice .objects .filter (registration_id__in = inactive_tokens ). update (
353
- active = False
354
- )
361
+ models .APNSDevice .objects .filter (
362
+ registration_id__in = inactive_tokens
363
+ ). update ( active = False )
355
364
356
365
if len (errors ) > 0 :
357
366
msg = "One or more errors failed with errors: {}" .format (", " .join (errors ))
@@ -371,6 +380,7 @@ async def _send_bulk_request(
371
380
topic : str = None ,
372
381
badge : int = None ,
373
382
sound : str = None ,
383
+ content_available : bool = None ,
374
384
extra : dict = {},
375
385
expiration : int = None ,
376
386
thread_id : str = None ,
@@ -391,19 +401,25 @@ async def _send_bulk_request(
391
401
if category :
392
402
aps_kwargs ["category" ] = category
393
403
394
- requests = [_create_notification_request_from_args (
395
- registration_id ,
396
- alert ,
397
- badge = badge ,
398
- sound = sound ,
399
- extra = extra ,
400
- expiration = expiration ,
401
- thread_id = thread_id ,
402
- loc_key = loc_key ,
403
- priority = priority ,
404
- collapse_id = collapse_id ,
405
- aps_kwargs = aps_kwargs
406
- ) for registration_id in registration_ids ]
404
+ if content_available :
405
+ aps_kwargs ["content-available" ] = 1
406
+
407
+ requests = [
408
+ _create_notification_request_from_args (
409
+ registration_id ,
410
+ alert ,
411
+ badge = badge ,
412
+ sound = sound ,
413
+ extra = extra ,
414
+ expiration = expiration ,
415
+ thread_id = thread_id ,
416
+ loc_key = loc_key ,
417
+ priority = priority ,
418
+ collapse_id = collapse_id ,
419
+ aps_kwargs = aps_kwargs ,
420
+ )
421
+ for registration_id in registration_ids
422
+ ]
407
423
408
424
send_requests = [_send_request (client , request ) for request in requests ]
409
425
return await asyncio .gather (* send_requests )
@@ -417,11 +433,11 @@ async def _send_request(apns, request):
417
433
return request .device_token , NotificationResult (
418
434
notification_id = request .notification_id ,
419
435
status = "failed" ,
420
- description = "TimeoutError"
436
+ description = "TimeoutError" ,
421
437
)
422
438
except :
423
439
return request .device_token , NotificationResult (
424
440
notification_id = request .notification_id ,
425
441
status = "failed" ,
426
- description = "CommunicationError"
442
+ description = "CommunicationError" ,
427
443
)
0 commit comments