【问题标题】:PyAPNs throws IOError Operation not supported by devicePyAPNs 抛出设备不支持的 IOError 操作
【发布时间】:2015-11-20 12:23:09
【问题描述】:

我正在尝试从本教程 raywenderlich 教程中为我的 iOS 推送应用程序实现 APNS

根据描述,我已经做了所有事情。 运行此命令后 openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem

我收到一条重要消息,表明一切都按预期连接。

我现在有两个文件 PushChatCert.pemPushChatKey.pemtoken_hex = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

所以为了从服务器发送推送通知,我关注PyAPNs

在我的 python shell 中完成所有这些操作后,它会抛出 IOError: [Errno 19] Operation not supported by device

import time
from apns import APNs, Frame, Payload
apns = APNs(use_sandbox=True, cert_file='PushChatCert.pem', key_file='PushChatKey.pem')

# Send a notification
token_hex = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
payload = Payload(alert="Hello World!", sound="default", badge=1)
apns.gateway_server.send_notification(token_hex, payload)

错误

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    apns.gateway_server.send_notification(token_hex, payload)
  File "/Library/Python/2.7/site-packages/apns.py", line 544, in send_notification
    self.write(self._get_notification(token_hex, payload))
  File "/Library/Python/2.7/site-packages/apns.py", line 273, in write
    return self._connection().write(string)
  File "/Library/Python/2.7/site-packages/apns.py", line 254, in _connection
    self._connect()
  File "/Library/Python/2.7/site-packages/apns.py", line 230, in _connect
    self._ssl = wrap_socket(self._socket, self.key_file, self.cert_file)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 891, in wrap_socket
    ciphers=ciphers)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 509, in __init__
    self._context.load_cert_chain(certfile, keyfile)
IOError: [Errno 19] Operation not supported by device

编辑:

在我的终端中运行此评论后 openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem收到委托问题如下

depth=1 /C=US/O=Entrust, Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C verify error:num=20:unable to get local issuer certificate

所以我下载了entrust_2048_ca.cer 文件并保存在同一个文件夹中,然后从我正在运行的终端中运行

openssl s_client -connect gateway.sandbox.push.apple.com:2195 -CAfile entrust_2048_ca.cer -cert PushChatCert.pem -key PushChatKey.pem

那么问题已经解决了,但是如何在PyAPNs中执行呢?

【问题讨论】:

    标签: python ios apple-push-notifications pyapns


    【解决方案1】:

    问题已解决。这是一些 SSL 安全问题,因为 python 无法访问该文件目录中的文件。

    我正在关注本教程apple-push-notification-services。 如本教程中所述,在创建 aps_development.cer 文件后,我正在执行这些操作以获取 .pem 文件

    $ openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem
    
    Convert the private key’s .p12 file into a .pem file:
    
    $ openssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12
    Enter Import Password: 
    MAC verified OK
    Enter PEM pass phrase: 
    Verifying - Enter PEM pass phrase:
    

    完成所有操作后,我拥有 PushChatKey.pemPushChatCert.pem 文件并使用这些文件,我无法将推送通知发送到我的设备 PyAPNs

    apns = APNs(use_sandbox=True, cert_file='PushChatKey.pem', key_file='PushChatCert.pem')
    

    我是怎么解决的?

    最后我从一开始就尝试了这些证书的创建,但这次是在其他一些教程之后

    Create your APNS certificates.

    创建名为aps_developer_identity.cer 的SSL 证书后下载。双击它以将其安装在 Keychain Access 应用程序中。您的提供商应用程序将使用 SSL 证书,以便它可以联系 APN 向您的应用程序发送推送通知。

    从本地 Mac 和“登录”钥匙串中启动钥匙串助手,按“证书”类别进行过滤。您将看到一个名为“Apple Development iOS Push Services”的可扩展选项:

    展开这个选项然后右击“Apple Development iOS Push Services” -> Export “Apple Development iOS Push Services ...”。将此文件另存为apns-dev-cert.p12 文件,您可以访问它。

    现在从 apns-dev-cert.p12 你在终端使用这些命令制作 .pem

    openssl pkcs12 -in apns-dev-cert.p12 -out apns.crt.pem -clcerts -nokeys
    openssl pkcs12 -in apns-dev-cert.p12 -out apns.key.pem -nocerts -nodes
    

    如果你想创建single .pem

    openssl pkcs12 -in apns-dev-cert.p12 -out apns_development.pem -nodes -clcerts
    

    现在将这些apns.crt.pemapns.key.pem 文件与PyAPNs 一起使用,它就像魔术一样工作。

    仔细创建您的provision profile

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      相关资源
      最近更新 更多