13
13
from . import models
14
14
from . import NotificationError
15
15
from .apns_errors import reason_for_exception_class
16
- from .settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS
16
+ from .conf import get_manager
17
17
18
18
19
19
class APNSError (NotificationError ):
@@ -30,20 +30,21 @@ def __init__(self, status):
30
30
self .status = status
31
31
32
32
33
- def _apns_create_socket (certfile = None ):
34
- certfile = certfile or SETTINGS . get ( "APNS_CERTIFICATE" )
33
+ def _apns_create_socket (certfile = None , application_id = None ):
34
+ certfile = certfile or get_manager (). get_apns_certificate ( application_id )
35
35
client = apns2_client .APNsClient (
36
36
certfile ,
37
- use_sandbox = SETTINGS .get ("APNS_USE_SANDBOX" ),
38
- use_alternative_port = SETTINGS .get ("APNS_USE_ALTERNATIVE_PORT" ))
37
+ use_sandbox = get_manager ().get_apns_use_sandbox (application_id ),
38
+ use_alternative_port = get_manager ().get_apns_use_alternative_port (application_id )
39
+ )
39
40
client .connect ()
40
41
return client
41
42
42
43
43
44
def _apns_prepare (
44
- token , alert , badge = None , sound = None , category = None , content_available = False ,
45
- action_loc_key = None , loc_key = None , loc_args = [], extra = {}, mutable_content = False ,
46
- thread_id = None , url_args = None ):
45
+ token , alert , application_id = None , badge = None , sound = None , category = None ,
46
+ content_available = False , action_loc_key = None , loc_key = None , loc_args = [],
47
+ extra = {}, mutable_content = False , thread_id = None , url_args = None ):
47
48
if action_loc_key or loc_key or loc_args :
48
49
apns2_alert = apns2_payload .PayloadAlert (
49
50
body = alert if alert else {}, body_localized_key = loc_key ,
@@ -59,8 +60,10 @@ def _apns_prepare(
59
60
url_args , custom = extra , thread_id = thread_id )
60
61
61
62
62
- def _apns_send (registration_id , alert , batch = False , ** kwargs ):
63
- client = _apns_create_socket (kwargs .pop ("certfile" , None ))
63
+ def _apns_send (
64
+ registration_id , alert , batch = False , application_id = None , certfile = None , ** kwargs
65
+ ):
66
+ client = _apns_create_socket (certfile = certfile , application_id = application_id )
64
67
65
68
notification_kwargs = {}
66
69
@@ -80,14 +83,19 @@ def _apns_send(registration_id, alert, batch=False, **kwargs):
80
83
data = [apns2_client .Notification (
81
84
token = rid , payload = _apns_prepare (rid , alert , ** kwargs )) for rid in registration_id ]
82
85
return client .send_notification_batch (
83
- data , SETTINGS .get ("APNS_TOPIC" ), ** notification_kwargs )
86
+ data , get_manager ().get_apns_topic (application_id = application_id ),
87
+ ** notification_kwargs
88
+ )
84
89
85
90
data = _apns_prepare (registration_id , alert , ** kwargs )
86
91
client .send_notification (
87
- registration_id , data , SETTINGS .get ("APNS_TOPIC" ), ** notification_kwargs )
92
+ registration_id , data ,
93
+ get_manager ().get_apns_topic (application_id = application_id ),
94
+ ** notification_kwargs
95
+ )
88
96
89
97
90
- def apns_send_message (registration_id , alert , ** kwargs ):
98
+ def apns_send_message (registration_id , alert , application_id = None , certfile = None , ** kwargs ):
91
99
"""
92
100
Sends an APNS notification to a single registration_id.
93
101
This will send the notification as form data.
@@ -100,7 +108,10 @@ def apns_send_message(registration_id, alert, **kwargs):
100
108
"""
101
109
102
110
try :
103
- _apns_send (registration_id , alert , ** kwargs )
111
+ _apns_send (
112
+ registration_id , alert , application_id = application_id ,
113
+ certfile = certfile , ** kwargs
114
+ )
104
115
except apns2_errors .APNsException as apns2_exception :
105
116
if isinstance (apns2_exception , apns2_errors .Unregistered ):
106
117
device = models .APNSDevice .objects .get (registration_id = registration_id )
@@ -109,7 +120,9 @@ def apns_send_message(registration_id, alert, **kwargs):
109
120
raise APNSServerError (status = reason_for_exception_class (apns2_exception .__class__ ))
110
121
111
122
112
- def apns_send_bulk_message (registration_ids , alert , ** kwargs ):
123
+ def apns_send_bulk_message (
124
+ registration_ids , alert , application_id = None , certfile = None , ** kwargs
125
+ ):
113
126
"""
114
127
Sends an APNS notification to one or more registration_ids.
115
128
The registration_ids argument needs to be a list.
@@ -119,7 +132,10 @@ def apns_send_bulk_message(registration_ids, alert, **kwargs):
119
132
to this for silent notifications.
120
133
"""
121
134
122
- results = _apns_send (registration_ids , alert , batch = True , ** kwargs )
135
+ results = _apns_send (
136
+ registration_ids , alert , batch = True , application_id = application_id ,
137
+ certfile = certfile , ** kwargs
138
+ )
123
139
inactive_tokens = [token for token , result in results .items () if result == "Unregistered" ]
124
140
models .APNSDevice .objects .filter (registration_id__in = inactive_tokens ).update (active = False )
125
141
return results
0 commit comments