且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Python加密有效载荷的Chrome Web推送通知

更新时间:2023-02-16 22:32:51

工作代码

  fcm_url =https://fcm.googleapis.com/fcm/send
encoded = {'body':i\x87\xb7W\xee\xd6QzL\xb6Q\xfe\ XF5 = t\xc4D [\xfe\xe3 + g\xb7\x86\xdd\x81\xb9I\xffX\x99\x9b\x85 $ x\x80\xc6 \x88\xe3\xbcm\x91\xff\x17a\x87C\x81\xf0\xbd\xb3} Y\xc8\xdb:\x14\x02\ xf2R\xe7\x12\xcb\x1c\x0f\x13\xca'\xec7B\xc3\x9e\xb2\x17\xa0\xf0\xcd\xed3 \xff\x1e\xc9k'A\xfb\x84\xf0\x17\xd4 + I\xe0\xe0\x92i%\x00\xf0\xe0\xdb [\xa5\xc9 /'\xc3L\xf6a\x183\xc1x7\x a6\x04!\xcctH\xf3\xf3\xcf\xf5\x12r\xd3\xf4\xd4\x8b\x10pc\x84Dd\xee\xe5'\ x82\xa8\x81\x9a\xf4\x94\xd3\x12\x166\x91G3t\x08\xf9\xbe\x1b\x90\x02\xc6\\ \\x17\x17\xc3\xe9\x08qQK\xd4\xce\xc2\x88\x8f\xcbA\xc9\xfd] \x99\x13\xfd\ xa6v2\xd65\x1b\xd5\x82)FUX\x92c\xed\xecF\x91rk\xba\x04\xd2\x90\x93a\\\
\x96 | M } \x10\xc4\xb0\xe4\xdd\x1dd4\xf1\xcb\x06< \xf7\x06\xf9\xfe\xce\x19W\xaa \xc4,'crypto_key':'BJUZv_0v3lqIkptS5R4r-DKbVXl4Cpd4YN4ASO4dcNzEGPgtNW7EDF2HBSNGy0fI8kBJj3bnSuCh0bmR46ICKpc','salt':'bjNe7m6zIQeDcsh4TF_wqg'}

crypto_key =dh =+ encoded [crypto_key]
salt = 盐=+ encoded ['salt']
headers = {'Authorization':'key ='+ fcm_key,'Content-Type':'application / json',}
headers.update({' crypto-key':crypto_key,'content-encoding':'aesgcm','encryption':salt})
fcm_data = {raw_data:base64.b64encode(encoded.get('body')) registration_ids:google_ids_array}
resp = requests.post(fcm_url,data = json.dumps(fcm_data),headers = headers)

对于编码有效载荷,您可以参考这个Python库: https ://github.com/web-push-libs/pywebpush



谢谢


Can someone please explain how to send Web Push Notification with Payload for Chrome using Curl/Python ?

This is the code I am trying:

Code:

fcm_url = "https://fcm.googleapis.com/fcm/send"
headers = {'Content-Type': 'application/json'}    
headers.update({'Authorization': 'key=' + fcm_key})

encoded = {'body': "i\x87\xb7W\xee\xd6QzL\xb6Q\xfe\xf5=t\xc4D[\xfe\xe3+g\xb7\x86\xdd\x81\xb9I\xffX\x99\x9b\x85$x\x80\xc6\x88\xe3\xbcm\x91\xff\x17a\x87C\x81\xf0\xbd\xb3}Y\xc8\xdb:\x14\x02\xf2R\xe7\x12\xcb\x1c\x0f\x13\xca'\xec7B\xc3\x9e\xb2\x17\xa0\xf0\xcd\xed3\xff\x1e\xc9k'A\xfb\x84\xf0\x17\xd4+I\xe0\xe0\x92i%\x00\xf0\xe0\xdb[\xa5\xc9/'\xc3L\xf6a\x183\xc1x7\xa6\x04!\xcctH\xf3\xf3\xcf\xf5\x12r\xd3\xf4\xd4\x8b\x10pc\x84Dd\xee\xe5'\x82\xa8\x81\x9a\xf4\x94\xd3\x12\x166\x91G3t\x08\xf9\xbe\x1b\x90\x02\xc6\x17\x17\xc3\xe9\x08qQK\xd4\xce\xc2\x88\x8f\xcbA\xc9\xfd]\x99\x13\xfd\xa6v2\xd65\x1b\xd5\x82)FUX\x92c\xed\xecF\x91rk\xba\x04\xd2\x90\x93a\n\x96|M}\x10\xc4\xb0\xe4\xdd\x1dd4\xf1\xcb\x06<\xf7\x06\xf9\xfe\xce\x19W\xaa\xc4", 'crypto_key': 'BJUZv_0v3lqIkptS5R4r-DKbVXl4Cpd4YN4ASO4dcNzEGPgtNW7EDF2HBSNGy0fI8kBJj3bnSuCh0bmR46ICKpc', 'salt': 'bjNe7m6zIQeDcsh4TF_wqg'}

crypto_key = "dh=" + encoded["crypto_key"]
salt = "salt=" + encoded['salt']
headers.update({'crypto-key': crypto_key, 'content-encoding': 'aesgcm', 'encryption': salt})    
fcm_data = {"data": {"message": base64.urlsafe_b64encode(encoded['body'])}, "registration_ids": google_ids_array}
resp = requests.post(fcm_url, data=json.dumps(fcm_data), headers=headers)

Response:

(200, 'OK')
{"multicast_id":66796434737498,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:14768189819%063a1cbcf9fd7ecd"}]}

but the notification doesn't contain the above specified Data.

Cases Tried, But None Useful:

1) fcm_data = {"data": {"message": "Hello"}, "to": google_ids_array[0]} # Wrong Notification
2) fcm_data = {"data": {"message": "Hello"}, "registration_ids": google_ids_array} # Wrong Notification
3) fcm_data = {"data": {"message": base64.urlsafe_b64encode(encoded['body'])}, "to": google_ids_array[0]} # Wrong Notification
4) fcm_data = {"data": {"message": base64.urlsafe_b64encode(encoded['body'])}, "registrations_ids": google_ids_array} # Bad Request, to

5) fcm_data = {"raw_data": "He", "registration_ids": google_ids_array} # success, No Notification
6) fcm_data = {"raw_data": str({"message": "Hello"}), "registration_ids": google_ids_array} # Failure, MessageTooBig
7) fcm_data = {"raw_data": base64.urlsafe_b64encode(encoded['body']), "registration_ids": google_ids_array} # Failure, MessageTooBig

Can someone please suggest how to send correct Web Notification with Payload on Chrome Browser ?

Any Suggestion/Hint would be helpful...

Thanks,

Working Code

fcm_url = "https://fcm.googleapis.com/fcm/send"        
encoded = {'body': "i\x87\xb7W\xee\xd6QzL\xb6Q\xfe\xf5=t\xc4D[\xfe\xe3+g\xb7\x86\xdd\x81\xb9I\xffX\x99\x9b\x85$x\x80\xc6\x88\xe3\xbcm\x91\xff\x17a\x87C\x81\xf0\xbd\xb3}Y\xc8\xdb:\x14\x02\xf2R\xe7\x12\xcb\x1c\x0f\x13\xca'\xec7B\xc3\x9e\xb2\x17\xa0\xf0\xcd\xed3\xff\x1e\xc9k'A\xfb\x84\xf0\x17\xd4+I\xe0\xe0\x92i%\x00\xf0\xe0\xdb[\xa5\xc9/'\xc3L\xf6a\x183\xc1x7\xa6\x04!\xcctH\xf3\xf3\xcf\xf5\x12r\xd3\xf4\xd4\x8b\x10pc\x84Dd\xee\xe5'\x82\xa8\x81\x9a\xf4\x94\xd3\x12\x166\x91G3t\x08\xf9\xbe\x1b\x90\x02\xc6\x17\x17\xc3\xe9\x08qQK\xd4\xce\xc2\x88\x8f\xcbA\xc9\xfd]\x99\x13\xfd\xa6v2\xd65\x1b\xd5\x82)FUX\x92c\xed\xecF\x91rk\xba\x04\xd2\x90\x93a\n\x96|M}\x10\xc4\xb0\xe4\xdd\x1dd4\xf1\xcb\x06<\xf7\x06\xf9\xfe\xce\x19W\xaa\xc4", 'crypto_key': 'BJUZv_0v3lqIkptS5R4r-DKbVXl4Cpd4YN4ASO4dcNzEGPgtNW7EDF2HBSNGy0fI8kBJj3bnSuCh0bmR46ICKpc', 'salt': 'bjNe7m6zIQeDcsh4TF_wqg'}

crypto_key = "dh=" + encoded["crypto_key"]
salt = "salt=" + encoded['salt']
headers = {'Authorization': 'key=' + fcm_key, 'Content-Type': 'application/json', }
headers.update({'crypto-key': crypto_key, 'content-encoding': 'aesgcm', 'encryption': salt})    
fcm_data = {"raw_data": base64.b64encode(encoded.get('body')), "registration_ids": google_ids_array}
resp = requests.post(fcm_url, data=json.dumps(fcm_data), headers=headers)

For Encoding The Payload, you can refer to this Python Library: https://github.com/web-push-libs/pywebpush

Thanks