订阅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)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java为什么在ng构建之后使用404?   java Maven原型“quickstart”无法扩展其POM   java Arduino端口卡在满功率状态   java如何确保在参数化构造函数失败时不调用默认构造函数?   java检测Blackberry中的切换失败   java如何将安卓库(作为Angle)链接到Eclipse项目?   java压缩图片,但获取“未知压缩方法”   java无法对应该初始化的模拟实例进行测试   将现有eclipse项目导出到war文件时,java“模块名无效”   java如何防止AppletClassLoader在查找类/其他资源时击中服务器?   java正则表达式根据分隔组的分隔符的数量获取n个组   java在JLayeredPane后面与JPanel交互   Java中的xml Soap请求   webView中的java utf8代码页