1

I am trying to implement APNS for my iOS push app from this tutorial raywenderlich tutorial.

As per the description I have done everything. After running this command openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem

I got a big message showing that everything is connected as expected.

I have two files now PushChatCert.pem and PushChatKey.pem and token_hex = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

So for sending push notification from server I following PyAPNs.

After doing all these in my python shell it is throwing 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)

Error

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

Edit :

After running this comment in my terminal openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem I was getting an entrust issue as follows

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

So I downloaded the entrust_2048_ca.cer file and kept in the same folder then from terminal I am running as

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

Then that problem has been solved but how to execute that in PyAPNs?

Kousik
  • 21,485
  • 7
  • 36
  • 59

1 Answers1

1

Problem has been solved. It was some SSL security issue for that python was not able to access the files from that file dir.

I was following this tutorial apple-push-notification-services. As described in this tutorial after creating the aps_development.cer file I was doing these to get the .pem files

$ 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:

After doing all I had PushChatKey.pem and PushChatCert.pem files and using those files I was not able to send push notifications to my device PyAPNs

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

How did I solve?

Finally I tried those certificate creation from the beginning but this time following some other tutorial

Create your APNS certificates.

After creating the SSL Certificate that you download as named aps_developer_identity.cer. Double-click on it to install it in the Keychain Access application. The SSL certificate will be used by your provider application so that it can contact the APNs to send push notifications to your applications.

Launch Keychain Assistant from your local Mac and from the 'login' keychain, filter by the 'Certificates' category. You will see an expandable option called “Apple Development iOS Push Services”:

Expand this option then right click on “Apple Development iOS Push Services” -> Export “Apple Development iOS Push Services ...″. Save this as apns-dev-cert.p12 file somewhere you can access it.

Now from apns-dev-cert.p12 you make .pem using these command from your terminal

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

If you want to create single .pem

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

Now use these apns.crt.pem and apns.key.pem files with PyAPNs and it works like magic.

Create your provision profile carefully.

Thanks.

Kousik
  • 21,485
  • 7
  • 36
  • 59