Django Push Notification - Can't read ANPS certificate file

861 Views Asked by At

I am trying to implement push notification on my iOS app that uses a Django backend. I will explain some context before, in case it has revealing information. If you want to go to the problem directly, go the The Problem section.

Context

Following this tutorial, I have been able to verify that my certificates and provision profiles are correctly configured, as my device is in fact receiving push notifications sent by APN Tester Free.

Of course, to make any sense, my Django backend will have to send the notifications. For this, I have installed django-push-notifications. In my django settings file, I have set up the paths to the APNS certificate. This file needs to be a .pem file instead of the normal .cer file that the Apple developer portal exports.

To convert the .cer file, one can right click the Push Notification Certificate in Keychain Access and export a .p12 file. To convert the .p12 file to the required .pem, one needs to use Terminal, and run the following command:

openssl pkcs12 -in [export p12 file].p12 -out [final pem file].pem -nodes -clcerts

To make sure, my .pem file is actually working, I used the following command

openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert [final pem file].pem -key [final pem file].pem

I seem to get a successful response (CONNECTED(00000003)...)

The Problem

After installing django-push-notification, even though I am able to create APNSDevice instances, I cannot send messages. This is the error I receive:

File "/Users/joao/Code/code-vitae.com/env/lib/python3.4/site-packages/push_notifications/models.py", line 93, in send_message
    return apns_send_message(registration_id=self.registration_id, alert=message, **kwargs)
  File "/Users/joao/Code/code-vitae.com/env/lib/python3.4/site-packages/push_notifications/apns.py", line 209, in apns_send_message
    _apns_send(registration_id, alert, **kwargs)
  File "/Users/joao/Code/code-vitae.com/env/lib/python3.4/site-packages/push_notifications/apns.py", line 153, in _apns_send
    with closing(_apns_create_socket_to_push()) as socket:
  File "/Users/joao/Code/code-vitae.com/env/lib/python3.4/site-packages/push_notifications/apns.py", line 58, in _apns_create_socket_to_push
    return _apns_create_socket((SETTINGS["APNS_HOST"], SETTINGS["APNS_PORT"]))
  File "/Users/joao/Code/code-vitae.com/env/lib/python3.4/site-packages/push_notifications/apns.py", line 46, in _apns_create_socket
    raise ImproperlyConfigured("The APNS certificate file at %r is not readable: %s" % (certfile, e))
django.core.exceptions.ImproperlyConfigured: The APNS certificate file at '/Users/joao/Documents/Circli/Certificates/Pro_Key.pem' is not readable: 'utf-8' codec can't decode byte 0xe3 in position 4783: invalid continuation byte

This error makes me suspect that I somehow made an error when creating the pem file. But how is this so, if I can connect to the Apple push servers without trouble (as shown in the Context)?

1

There are 1 best solutions below

1
On

Looks like there is a problem in the process that you use to convert the certificates.

Use fastlane to generate push certificates in needed format right away. Here is a blog post covering whole process