官方文檔地址:ios notification
推送截圖步驟:
推送接入方法蘋果給出了兩種:
- certificate 證書接入
- token 接入
token 接入方法
- 根據文檔給出的簽名規則獲取簽名token。
import jwt
import time
token_dict = {
'iat': int(time.time()),
'iss': '**********' #頒發者密鑰,其值是您用於開發公司應用程序的10個字符的團隊ID。從您的開發人員帳戶獲取此值
}
header = {
'alg': "ES256", # 指定的加密方法
'kid': "**********" # 您從開發者帳戶獲得的10個字符的密鑰ID
}
signing_pem = open("AuthKey_WW37W62BVM.p8", 'r').read() # 身份驗證令牌簽名密鑰,指定爲文本文件(帶有.p8文件擴展名)。
jwt_token = jwt.encode(token_dict, # payload, 有效載體
signing_pem, # 進行加密簽名的密鑰
algorithm="ES256", # 指明簽名算法方式, 默認也是HS256
headers=header # json web token 數據結構包含兩部分, payload(有效載體), headers(標頭)
).decode('ascii') # python3 編碼後得到 bytes, 再進行解碼(指明解碼的格式), 得到一個str
print(jwt_token)
- 根據獲取的token,發現請求到對應接口(蘋果支支持HTTP2請求,普通的請求HTTP1會報錯)。
import requests
import json
test_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 HTTP20Adapter
sessions = requests.session()
path = "/3/device/6211b0b7a393c90cc770c7d8a6c54ef760c22a5df1d06eea4778b7d4d614494a"
url_full = test_url + path
data = {
"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:靜默提示
其他參數說明