From 207e47703dbd054833bebc547949bd7173989997 Mon Sep 17 00:00:00 2001 From: Phishion Date: Thu, 24 Feb 2022 22:16:05 +0800 Subject: [PATCH] pass alert parameter as a function which accepts token parameter in order to set different message per user --- README.rst | 10 ++++++++++ push_notifications/apns.py | 3 +++ 2 files changed, 13 insertions(+) diff --git a/README.rst b/README.rst index d1939821..847fef57 100644 --- a/README.rst +++ b/README.rst @@ -411,6 +411,16 @@ value per user. Assuming User model has a method get_badge returning badge count badge=lambda token: APNSDevice.objects.get(registration_id=token).user.get_badge() ) +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 + ) + Firebase vs Google Cloud Messaging ---------------------------------- diff --git a/push_notifications/apns.py b/push_notifications/apns.py index 04064872..5fbe9361 100644 --- a/push_notifications/apns.py +++ b/push_notifications/apns.py @@ -42,6 +42,9 @@ 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,