I've been trying to access the APNS in order to send push notifications to an iOS app I develop. I have created a development and production certificate requests and signed them on the Apple dev center:
For each certificate (production and development), I have three files:
aps_development.cer: A
.cer` file I have downloaded from Apple after signing the certificate request.dev.pem
: A .pem
public key file from the OSX keychain.dev.p12
: A .p12
private key file from the OSX keychain, with password (and dev_nopass.p12
: A .p12
private key file from the OSX keychain, with password).from apns import APNs, Frame, Payload
p12 = '/path/to/dev.p12'
p12_nopass = '/path/to/dev_nopass.p12'
pem = '/path/to/dev.pem'
cer = '/path/to/aps_development.cer'
apns = APNs(use_sandbox=True,
cert_file=pem,
key_file=p12_nopass)
# Send a notification
# Dummy token, but it does not cause the error (SSL fails before token check)
token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87'
payload = Payload(alert="Hello World!", sound="default", badge=1)
apns.gateway_server.send_notification(token_hex, payload)
Result:
Traceback (most recent call last):
File "apple_push_driver.py", line 18, in <module>
apns.gateway_server.send_notification(token_hex, payload)
File "/path_to/site-packages/apns.py", line 381, in send_notification
self.write(self._get_notification(token_hex, payload))
File "/path_to/apns.py", line 174, in write
return self._connection().write(string)
File "/path_to/apns.py", line 167, in _connection
self._connect()
File "/path_to/apns.py", line 151, in _connect
self._ssl = wrap_socket(self._socket, self.key_file, self.cert_file)
File "/path_to/ssl.py", line 387, in wrap_socket
ciphers=ciphers)
File "/path_to/ssl.py", line 141, in __init__
ciphers)
ssl.SSLError: [Errno 336265225] _ssl.c:351: error:140B0009:SSL routines:SSL_CTX_use_PrivateKey_file:PEM lib
I tried both p12
and p12_nopass
, production and dev, and got the same results.
Any idea why I can't make SSL connection in order to send a Push notification via PyAPNs?
The problem was using the original .p12
/ cer
. files. They should be converted to pem
file, using the fine instructions here.