@@ -47,7 +47,7 @@ def _gcm_send(data, content_type):
4747 return urlopen (request ).read ()
4848
4949
50- def _gcm_send_plain (registration_id , data , collapse_key = None , delay_while_idle = False , time_to_live = 0 ):
50+ def _gcm_send_plain (registration_id , data , ** kwargs ):
5151 """
5252 Sends a GCM notification to a single registration_id.
5353 This will send the notification as form data.
@@ -57,18 +57,16 @@ def _gcm_send_plain(registration_id, data, collapse_key=None, delay_while_idle=F
5757
5858 values = {"registration_id" : registration_id }
5959
60- if collapse_key :
61- values ["collapse_key" ] = collapse_key
62-
63- if delay_while_idle :
64- values ["delay_while_idle" ] = int (delay_while_idle )
65-
66- if time_to_live :
67- values ["time_to_live" ] = time_to_live
68-
6960 for k , v in data .items ():
7061 values ["data.%s" % (k )] = v .encode ("utf-8" )
7162
63+ for k , v in kwargs .items ():
64+ if v :
65+ if isinstance (v , bool ):
66+ # Encode bools into ints
67+ v = 1
68+ values [k ] = v
69+
7270 data = urlencode (sorted (values .items ())).encode ("utf-8" ) # sorted items for tests
7371
7472 result = _gcm_send (data , "application/x-www-form-urlencoded;charset=UTF-8" )
@@ -77,7 +75,7 @@ def _gcm_send_plain(registration_id, data, collapse_key=None, delay_while_idle=F
7775 return result
7876
7977
80- def _gcm_send_json (registration_ids , data , collapse_key = None , delay_while_idle = False , time_to_live = 0 ):
78+ def _gcm_send_json (registration_ids , data , ** kwargs ):
8179 """
8280 Sends a GCM notification to one or more registration_ids. The registration_ids
8381 needs to be a list.
@@ -89,14 +87,9 @@ def _gcm_send_json(registration_ids, data, collapse_key=None, delay_while_idle=F
8987 if data is not None :
9088 values ["data" ] = data
9189
92- if collapse_key :
93- values ["collapse_key" ] = collapse_key
94-
95- if delay_while_idle :
96- values ["delay_while_idle" ] = delay_while_idle
97-
98- if time_to_live :
99- values ["time_to_live" ] = time_to_live
90+ for k , v in kwargs .items ():
91+ if v :
92+ values [k ] = v
10093
10194 data = json .dumps (values , separators = ("," , ":" ), sort_keys = True ).encode ("utf-8" ) # keys sorted for tests
10295
@@ -106,7 +99,7 @@ def _gcm_send_json(registration_ids, data, collapse_key=None, delay_while_idle=F
10699 return result
107100
108101
109- def gcm_send_message (registration_id , data , collapse_key = None , delay_while_idle = False , time_to_live = 0 ):
102+ def gcm_send_message (registration_id , data , ** kwargs ):
110103 """
111104 Sends a GCM notification to a single registration_id.
112105
@@ -115,32 +108,34 @@ def gcm_send_message(registration_id, data, collapse_key=None, delay_while_idle=
115108
116109 If sending multiple notifications, it is more efficient to use
117110 gcm_send_bulk_message() with a list of registration_ids
118- """
119111
120- args = data , collapse_key , delay_while_idle , time_to_live
112+ A reference of extra keyword arguments sent to the server is available here:
113+ https://developers.google.com/cloud-messaging/server-ref#downstream
114+ """
121115
122116 try :
123- _gcm_send_plain (registration_id , * args )
117+ _gcm_send_plain (registration_id , data , ** kwargs )
124118 except AttributeError :
125- _gcm_send_json ([registration_id ], * args )
119+ _gcm_send_json ([registration_id ], data , * kwargs )
126120
127121
128- def gcm_send_bulk_message (registration_ids , data , collapse_key = None , delay_while_idle = False , time_to_live = 0 ):
122+ def gcm_send_bulk_message (registration_ids , data , ** kwargs ):
129123 """
130124 Sends a GCM notification to one or more registration_ids. The registration_ids
131125 needs to be a list.
132126 This will send the notification as json data.
133- """
134127
135- args = data , collapse_key , delay_while_idle , time_to_live
128+ A reference of extra keyword arguments sent to the server is available here:
129+ https://developers.google.com/cloud-messaging/server-ref#downstream
130+ """
136131
137132 # GCM only allows up to 1000 reg ids per bulk message
138133 # https://developer.android.com/google/gcm/gcm.html#request
139134 max_recipients = SETTINGS .get ("GCM_MAX_RECIPIENTS" )
140135 if len (registration_ids ) > max_recipients :
141136 ret = []
142137 for chunk in _chunks (registration_ids , max_recipients ):
143- ret .append (_gcm_send_json (chunk , * args ))
138+ ret .append (_gcm_send_json (chunk , data , ** kwargs ))
144139 return ret
145140
146- return _gcm_send_json (registration_ids , * args )
141+ return _gcm_send_json (registration_ids , data , ** kwargs )
0 commit comments