Benjamin

静以修身,俭以养德,非澹薄无以明志,非宁静无以致远。
随笔 - 388, 文章 - 0, 评论 - 196, 引用 - 0
数据加载中……

Python使用PyAPNs搭建APNS推送通知

APNS 是苹果为IOS设备提供的推送服务,全称是(Apple Push Notification service)
从github上下载源码: PyAPNs https://github.com/djacobs/PyAPNs
然后将解压文件夹中的apns.py放入你的项目中.
#测试接口(同步阻塞)
def ios_push_test(token,in_alert,cert_path,key_path):
    # Send a notification
    apns = APNs(cert_file=cert_path,key_file=key_path)
    token_hex = token
    payload = Payload(alert=in_alert, sound="default", badge=1)
    apns.gateway_server.send_notification(token_hex, payload)
#测试接口(带相应回调:异步非阻塞)
def ios_test(token,cert_path,key_path,in_alert):
    def response_listener(error_response):
        print("client get error-response: " + str(error_response))
    #use_sandbox默认值为False,表示生产环境;Ture则表示开发(测试)环境
    apns_enhanced = APNs(use_sandbox=True,cert_file=cert_path, key_file=key_path,enhanced=True)
    token_hex = token
    payload = Payload(alert=in_alert, sound="default", badge=1)
    identifier = random.getrandbits(32)
    print(identifier)
    apns_enhanced.gateway_server.register_response_listener(response_listener)
    apns_enhanced.gateway_server.send_notification(token_hex, payload, identifier=identifier)
错误代码
状态码 描述
0 正确
1 处理过程错误
2 缺少设备令牌
3 缺少主题
4 缺少有效消息体
5 令牌长度错误
6 大小错误
7 消息体长度错误
8 令牌错误
255 未知错误

posted on 2020-04-22 11:11 Benjamin 阅读(508) 评论(0)  编辑 收藏 引用 所属分类: python


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理