How to create Push notifications PEM file

iOS Push Notifications allow apps to receive updates, alerts, or other messages from a server, even when the app is not running. The notifications can be delivered to the user's device and appear as badges, sounds, or alerts. Below is a comprehensive guide on the process of implementing iOS Push Notifications, from configuring your app to sending notifications.

Prerequisite

  1. For Development purpose (compiled through Xcode), in Ayla Developer Dashboard, select the Use sandbox for apns option, and upload Apple Development PEM file.

  2. For Production purpose (TestFlight/Hocky App Release), in Ayla Developer Dashboard, clear the Use sandbox for apns option, and upload Apple Production PEM file.

  3. Click "+" to upload the APNS PEM.

  4. In the Name field, enter the certificate name.

  5. Click Choose file to browse and select the PEM file.

  6. In the Passphrase field, enter the certificate password, and then click CREATE.
    Once the information above is entered, the screen will appear as follows:

NOTE: If the PEM file has a passphrase, you must enter it when uploading the file to the Ayla Dashboard.

Configuring App for Push Notifications

  1. Enable Push Notifications in Xcode
    1. Open your app's project in Xcode.
    2. Go to Signing & Capabilities.
    3. Under the Capabilities section, add Push Notifications.
  2. Create an App ID in Apple Developer Portal
    1. Visit the Apple Developer Portal.
    2. Go to Certificates, Identifiers & Profiles > Identifiers.
    3. Create a new App ID or edit an existing one.
    4. Ensure Push Notifications is enabled for your app's App ID.

Create APNs Authentication Key or APNs SSL Certificate

You need to create either an APNs Authentication Key (recommended) or an APNs SSL Certificate. These are used by your server to authenticate when sending push notifications to APNs.

For details on how to configure push notifications Please refer to iOS remote push notifications in a nutshell.

Steps to create a PEM file

  1. Login to Apple developer account and go to Certificates section

  2. Create a new certificate.

  3. Create a CSR using Keychain
    On the Keychain Access menu, select Request a Certificate from a certificate authority….

    Enter your email id and select the Saved to disk option.


  4. On the Certificates, Identifiers & Profiles page, upload the .certSigningCertificate, click Continue.

  5. Click Download. The .cer file will be downloaded.

  6. Go to the Downloads folder and double click the .cer file, it will open in the Keychain access.

  7. Double-click the certificate to trust it and and select Always Trust. Enter system password to confirm the same.

  8. After following the above steps, the screen looks as shown in the following screenshot. You will see the message “This certificate is marked as trusted for this account”.

  9. Right-click on the newly created Push Service certificate along with the private key, and select Export to generate a .p12 file.

  10. Enter the password for the .p12 certificate and lick OK.

  1. Generate the .pem file using following commands. This generates a PEM file which is a combination of certificate-key pair.

    openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert Company_PN_Prod_Nov_2024_Cert.pem -key Company_PN_Prod_Nov_2024_key.pem
    openssl s_client -connect gateway.push.apple.com:2195 -cert Company_PN_Prod_Nov_2024_Cert.pem -key Company_PN_Prod_Nov_2024_key.pem
    
  2. Verify that the certificate is valid by using the following commands.

    openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert Company_PN_Prod_Feb_2015_Cert.pem -key Company_PN_Prod_Feb_2015_key.pem
    openssl s_client -connect gateway.push.apple.com:2195 -cert Company_PN_Prod_Feb_2015_Cert.pem -key Company_PN_Prod_Feb_2015_key.pem
    

After the successful verification, we get the output as follows: