The APNS_CERTIFICATE setting requires a path to a .pem file containing both a certificate and a private key pair. This allows a secure connection to Apple's push gateway.
1. Generate the Apple Push Certificate
Use the Apple Developer site to generate a push notification certificate for either development or production.
Important: To avoid confusion between sandbox and production certificates, do not generate the certificate from the top-level Certificates section. Instead, navigate through your specific app's configuration:
Identifiers -> App IDs -> [Your App] -> Edit -> Push Notifications Section -> Create Certificate.
The end result should be an exported .p12 file containing the certificate and private key.
2. Convert the .p12 certificate to PEM format
Follow these steps using openssl to prepare the file for the library:
- Extract the certificate:
openssl pkcs12 -clcerts -nokeys -out aps-cert.pem -in Certificates.p12
- Extract the key:
openssl pkcs12 -nocerts -out aps-key.pem -in Certificates.p12
- Remove the passphrase from the key:
openssl rsa -in aps-key.pem -out aps-key-noenc.pem
- Combine certificate and key into a single file:
cat aps-cert.pem aps-key-noenc.pem > aps.pem
3. Verify connectivity
Test the validity of your certificate and connectivity to the APNS gateway:
Production:
openssl s_client -connect gateway.push.apple.com:2195 -cert aps-cert.pem -key aps-key-noenc.pem
Sandbox:
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert aps-cert.pem -key aps-key-noenc.pem
If the connection opens and remains open, the certificate is valid. If it closes or displays an error, the certificate or key is invalid.
$ openssl pkcs12 -clcerts -nokeys -out aps-cert.pem -in Certificates.p12
$ openssl pkcs12 -nocerts -out aps-key.pem -in Certificates.p12
$ openssl rsa -in aps-key.pem -out aps-key-noenc.pem
$ cat aps-cert.pem aps-key-noenc.pem > aps.pem