订阅GCM/FCM并接收通知
push-receiver的Python项目详细描述
订阅GCM/FCM并接收通知
https://github.com/MatthieuLemoine/push-receiver的python实现
在Python2.7.16、3.4.10和3.7.5上测试
我用了一天左右就把它拼好了,它的边缘还很粗糙, 尤其是倾听部分,我并不真正利用自己,只是 只是为了好玩而实现的,只是经过短暂的测试
注意,对于监听部分,我必须引入httpece,这取决于 在一个成熟的本地加密库而不仅仅是oscrypto。它是 一个可选的依赖项,因此您必须通过 按下接收器[听]
使用
pip install push_receiver[listen,example]
存储和加载凭据和持久ID的基本用法示例 并打印新通知
您也可以使用以下命令运行此示例(更改发件人id)
^{pr2}$frompush_receiverimportregister,listenimportjsondefon_notification(obj,notification,data_message):idstr=data_message.persistent_id+"\n"# check if we already received the notificationwithopen("persistent_ids.txt","r")asf:ifidstrinf:return# new notification, store id so we don't read it againwithopen("persistent_ids.txt","a")asf:f.write(idstr)# print notificationn=notification["notification"]text=n["title"]ifn["body"]:text+=": "+n["body"]print(text)if__name__=="__main__":SENDER_ID=722915550290# change this to your sender idtry:# already registered, load previous credentialswithopen("credentials.json","r")asf:credentials=json.load(f)exceptFileNotFoundError:# first time, register and store credentialscredentials=register(sender_id=SENDER_ID)withopen("credentials.json","w")asf:json.dump(credentials,f)print("send notifications to {}".format(credentials["fcm"]["token"]))withopen("persistent_ids.txt","a+")asf:received_persistent_ids=[x.strip()forxinf]listen(credentials,on_notification,received_persistent_ids)
- 项目
标签: