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 未知错误