@@ -152,7 +152,7 @@ def encode(self, data):
152152 'body' : encrypted ,
153153 })
154154
155- def send (self , data , headers = None , ttl = 0 , gcm_key = None , reg_id = None ):
155+ def send (self , data = None , headers = None , ttl = 0 , gcm_key = None , reg_id = None ):
156156 """Encode and send the data to the Push Service.
157157
158158 :param data: A serialized block of data (see encode() ).
@@ -169,22 +169,25 @@ def send(self, data, headers=None, ttl=0, gcm_key=None, reg_id=None):
169169 # Encode the data.
170170 if headers is None :
171171 headers = dict ()
172- encoded = self .encode (data )
173- # Append the p256dh to the end of any existing crypto-key
172+ encoded = {}
174173 headers = CaseInsensitiveDict (headers )
175- crypto_key = headers .get ("crypto-key" , "" )
176- if crypto_key :
177- # due to some confusion by a push service provider, we should
178- # use ';' instead of ',' to append the headers.
179- # see https://github.com/webpush-wg/webpush-encryption/issues/6
180- crypto_key += ';'
181- crypto_key += "keyid=p256dh;dh=" + encoded ["crypto_key" ].decode ('utf8' )
182- headers .update ({
183- 'crypto-key' : crypto_key ,
184- 'content-encoding' : 'aesgcm' ,
185- 'encryption' : "keyid=p256dh;salt=" +
186- encoded ['salt' ].decode ('utf8' ),
187- })
174+ if data :
175+ encoded = self .encode (data )
176+ # Append the p256dh to the end of any existing crypto-key
177+ crypto_key = headers .get ("crypto-key" , "" )
178+ if crypto_key :
179+ # due to some confusion by a push service provider, we should
180+ # use ';' instead of ',' to append the headers.
181+ # see https://github.com/webpush-wg/webpush-encryption/issues/6
182+ crypto_key += ';'
183+ crypto_key += (
184+ "keyid=p256dh;dh=" + encoded ["crypto_key" ].decode ('utf8' ))
185+ headers .update ({
186+ 'crypto-key' : crypto_key ,
187+ 'content-encoding' : 'aesgcm' ,
188+ 'encryption' : "keyid=p256dh;salt=" +
189+ encoded ['salt' ].decode ('utf8' ),
190+ })
188191 gcm_endpoint = 'https://android.googleapis.com/gcm/send'
189192 if self .subscription_info ['endpoint' ].startswith (gcm_endpoint ):
190193 if not gcm_key :
@@ -194,13 +197,14 @@ def send(self, data, headers=None, ttl=0, gcm_key=None, reg_id=None):
194197 if not reg_id :
195198 reg_id = self .subscription_info ['endpoint' ].rsplit ('/' , 1 )[- 1 ]
196199 reg_ids .append (reg_id )
197- data = dict ()
198- data ['registration_ids' ] = reg_ids
199- data ['raw_data' ] = base64 .b64encode (
200- encoded .get ('body' )).decode ('utf8' )
201- data ['time_to_live' ] = int (
200+ gcm_data = dict ()
201+ gcm_data ['registration_ids' ] = reg_ids
202+ if data :
203+ gcm_data ['raw_data' ] = base64 .b64encode (
204+ encoded .get ('body' )).decode ('utf8' )
205+ gcm_data ['time_to_live' ] = int (
202206 headers ['ttl' ] if 'ttl' in headers else ttl )
203- encoded_data = json .dumps (data )
207+ encoded_data = json .dumps (gcm_data )
204208 headers .update ({
205209 'Authorization' : 'key=' + gcm_key ,
206210 'Content-Type' : 'application/json' ,
0 commit comments