使用Python加密有效载荷的Chrome Web推送通知 [英] Chrome Web Push Notification with Encrypted Payload in Python

查看:605
本文介绍了使用Python加密有效载荷的Chrome Web推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释如何使用Curl / Python发送带有Payload for Chrome的Web推送通知?



这是我正在尝试的代码:



代码:

  fcm_url =https://fcm.googleapis.com/fcm /发送
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\\ XD 4 + 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\\\
\x96 | M} \\ \\x10\xc4\xb0\xe4\xdd\x1dd4\xf1\xcb\x06< \xf7\x06\xf9\xfe\xce\x19W\xaa\ xc4,'crypto_key':'BJUZv_0v3lqIkptS5R4r-DKbVXl4Cpd4YN4ASO4dcNzEGPgtNW7EDF2HBSNGy0f I8kBJj3bnSuCh0bmR46ICKpc','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)

回应:

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

但通知不包含上述指定的数据。



案例尝试,但无有用:

  1)fcm_data = {data {message:Hello},to:google_ids_array [0]}#错误通知
2)fcm_data = {data:{message:Hello},registration_ids:google_ids_array}#错误通知
3)fcm_data = {data:{message:base64.urlsafe_b64encode '])},to:google_ids_array [0]}#错误通知
4)fcm_data = {data:{message:base64.urlsafe_b64encode(encoded ['body'])},registrations_ids :google_ids_array}#不正确请求,

5)fcm_data = {raw_data:他,registration_ids:google_ids_array}#成功,否通知
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}#失败,MessageTooBig

有人可以建议如何发送正确的Web在Chrome浏览器上使用Payload通知?



任何建议/提示都会有帮助...



谢谢,

解决方案

工作代码

  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

这篇关于使用Python加密有效载荷的Chrome Web推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆