美文网首页
Python进行iOS推送(APNS)

Python进行iOS推送(APNS)

作者: 冷煖自知 | 来源:发表于2019-11-22 09:52 被阅读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

    相关文章

      网友评论

          本文标题:Python进行iOS推送(APNS)

          本文链接:https://www.haomeiwen.com/subject/qqdfwctx.html