Python进行iOS推送(APNS)
2019-11-22 本文已影响0人
冷煖自知
Python服务端
下载PyAPNs
安装python setup.py install
Python文件
import os
from apns import APNs, Frame, Payload
apn = APNs(use_sandbox=True, cert_file=os.path.join('', 'apns-dev-cert.pem'), key_file=os.path.join('', 'dev-key-noec.pem'))
token_hex = '<Your DeviceToken>'
payload = Payload(alert="Hello World!", sound="default", badge=1)
apn.gateway_server.send_notification(token_hex, payload)
Swift DeviceToken获取
// 注册远程通知
UIApplication.shared.registerForRemoteNotifications()
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
var deviceTokenString = String()
let bytes = [UInt8](deviceToken)
for item in bytes {
deviceTokenString += String(format:"%02x", item&0x000000FF)
}
print("deviceToken:\(deviceTokenString)")
}
关于推送证书pem生成
- 将下载的cer证书安装到钥匙串,导出公钥
apns-dev-cert.p12
和私钥apns-dev-key.p12
- 在终端输入
openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p12
导出公钥apns-dev-cert.pem
-
openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-key.p12
导出私钥apns-dev-key.pem
- 如果去除私钥密码
openssl rsa -in apns-dev-key.pem -out dev-key-noec.pem
- 如果需要合成
cat apns-dev-cert.pem apns-dev-key.pem > apns-dev.pem
- 验证pem文件
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert apns-dev-cert.pem -key apns-dev-key.pem