From cdbc67a9ad15a9be433a0f877625adf0b8dc7160 Mon Sep 17 00:00:00 2001 From: Phishion Date: Sun, 14 Nov 2021 16:35:37 +0800 Subject: [PATCH 1/2] pass alert parameter as a function which accepts token parameter in order to set different message per user --- push_notifications/apns.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/push_notifications/apns.py b/push_notifications/apns.py index 04064872..160aa789 100644 --- a/push_notifications/apns.py +++ b/push_notifications/apns.py @@ -42,20 +42,23 @@ def _apns_prepare( token, alert, application_id=None, badge=None, sound=None, category=None, content_available=False, action_loc_key=None, loc_key=None, loc_args=[], extra={}, mutable_content=False, thread_id=None, url_args=None): - if action_loc_key or loc_key or loc_args: - apns2_alert = apns2_payload.PayloadAlert( - body=alert if alert else {}, body_localized_key=loc_key, - body_localized_args=loc_args, action_localized_key=action_loc_key) - else: - apns2_alert = alert - - if callable(badge): - badge = badge(token) - - return apns2_payload.Payload( - alert=apns2_alert, badge=badge, sound=sound, category=category, - url_args=url_args, custom=extra, thread_id=thread_id, - content_available=content_available, mutable_content=mutable_content) + if callable(alert): + alert = alert(token) + + if action_loc_key or loc_key or loc_args: + apns2_alert = apns2_payload.PayloadAlert( + body=alert if alert else {}, body_localized_key=loc_key, + body_localized_args=loc_args, action_localized_key=action_loc_key) + else: + apns2_alert = alert + + if callable(badge): + badge = badge(token) + + return apns2_payload.Payload( + alert=apns2_alert, badge=badge, sound=sound, category=category, + url_args=url_args, custom=extra, thread_id=thread_id, + content_available=content_available, mutable_content=mutable_content) def _apns_send( From b19e8816773c7683a3f33e1f3b738ce68eeb7c5a Mon Sep 17 00:00:00 2001 From: Phishion Date: Thu, 24 Feb 2022 14:04:48 +0800 Subject: [PATCH 2/2] Update apns.py The original code will prompt an indentation warning in my IDE (PyCharm), so I modified it, but both indentation can be run correctly, Readme I have written Well, after you pass, you can insert by yourself after line 413 of the current file (before "Firebase vs Google Cloud Messaging") ============================================================================================================ Similar to the above, It's also possible to pass message parameter as a function which accepts token parameter in order to set different message value per user. Assuming User model has a method get_message returning message for a user: .. code-block:: python devices.send_message( message=lambda token: APNSDevice.objects.get(registration_id=token).user.get_message() badge=5 ) --- push_notifications/apns.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/push_notifications/apns.py b/push_notifications/apns.py index 160aa789..b325581b 100644 --- a/push_notifications/apns.py +++ b/push_notifications/apns.py @@ -42,23 +42,23 @@ def _apns_prepare( token, alert, application_id=None, badge=None, sound=None, category=None, content_available=False, action_loc_key=None, loc_key=None, loc_args=[], extra={}, mutable_content=False, thread_id=None, url_args=None): - if callable(alert): - alert = alert(token) - - if action_loc_key or loc_key or loc_args: - apns2_alert = apns2_payload.PayloadAlert( - body=alert if alert else {}, body_localized_key=loc_key, - body_localized_args=loc_args, action_localized_key=action_loc_key) - else: - apns2_alert = alert - - if callable(badge): - badge = badge(token) - - return apns2_payload.Payload( - alert=apns2_alert, badge=badge, sound=sound, category=category, - url_args=url_args, custom=extra, thread_id=thread_id, - content_available=content_available, mutable_content=mutable_content) + if callable(alert): + alert = alert(token) + + if action_loc_key or loc_key or loc_args: + apns2_alert = apns2_payload.PayloadAlert( + body=alert if alert else {}, body_localized_key=loc_key, + body_localized_args=loc_args, action_localized_key=action_loc_key) + else: + apns2_alert = alert + + if callable(badge): + badge = badge(token) + + return apns2_payload.Payload( + alert=apns2_alert, badge=badge, sound=sound, category=category, + url_args=url_args, custom=extra, thread_id=thread_id, + content_available=content_available, mutable_content=mutable_content) def _apns_send(