APNS 是蘋(píng)果為IOS設(shè)備提供的推送服務(wù),全稱(chēng)是(Apple Push Notification service)
從github上下載源碼: PyAPNs https://github.com/djacobs/PyAPNs
然后將解壓文件夾中的apns.py放入你的項(xiàng)目中.
#測(cè)試接口(同步阻塞)
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)
#測(cè)試接口(帶相應(yīng)回調(diào):異步非阻塞)
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默認(rèn)值為False,表示生產(chǎn)環(huán)境;Ture則表示開(kāi)發(fā)(測(cè)試)環(huán)境
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)
錯(cuò)誤代碼
狀態(tài)碼 描述
0 正確
1 處理過(guò)程錯(cuò)誤
2 缺少設(shè)備令牌
3 缺少主題
4 缺少有效消息體
5 令牌長(zhǎng)度錯(cuò)誤
6 大小錯(cuò)誤
7 消息體長(zhǎng)度錯(cuò)誤
8 令牌錯(cuò)誤
255 未知錯(cuò)誤