生活随笔
收集整理的這篇文章主要介紹了
python3 接入IOS推送apn
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
官方文檔地址:ios notification
推送截圖步驟:
推送接入方法蘋果給出了兩種:
certificate 證書接入token 接入
token 接入方法
根據文檔給出的簽名規則獲取簽名token。
import jwt
import time
token_dict
= {'iat': int(time
.time
()),'iss': '**********'
}
header
= {'alg': "ES256", 'kid': "**********"
}
signing_pem
= open("AuthKey_WW37W62BVM.p8", 'r').read
() jwt_token
= jwt
.encode
(token_dict
, signing_pem
, algorithm
="ES256", headers
=header
).decode
('ascii') print(jwt_token
)
根據獲取的token,發現請求到對應接口(蘋果支支持HTTP2請求,普通的請求HTTP1會報錯)。
import requests
import jsontest_url
= "https://api.sandbox.push.apple.com"
product_url
= "https://api.push.apple.com"def get_headers():headers
= {"authorization": "bearer " + jwt_token
,"apns-push-type": "alert","apns-topic": "com.sxzq.ficc.fawo"}return headers
def post_data():from hyper
.contrib
import HTTP20Adaptersessions
= requests
.session
()path
= "/3/device/6211b0b7a393c90cc770c7d8a6c54ef760c22a5df1d06eea4778b7d4d614494a"url_full
= test_url
+ pathdata
= {"aps": {"alert": "測試通知","sound": "default","badge":4}}print(url_full
)sessions
.mount
('https://api.sandbox.push.apple.com', HTTP20Adapter
())r
= sessions
.post
(url_full
, data
=json
.dumps
(data
), headers
=get_headers
())print(r
.content
)print(r
.status_code
)print(r
.headers
)if __name__
== "__main__":post_data
()
其他參數說明:
apns-expiration:過期
apns-priority:優先權
apns-collapse-id:合并
content-available:靜默提示
其他參數說明
總結
以上是生活随笔為你收集整理的python3 接入IOS推送apn的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。