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 未知錯誤