Push Notifications: How to Generate .pem file

iOS Tutorials: Push Notification Featured Image

What are Push Notifications?

Push notifications is a kind of message which an App publisher wants to notify a user. It is a server pushed message.

For Example, there is a mobile application which belongs to News domain. An application of such a domain needs to update its user with the happenings in the world as earliest as possible to stand in the market. and their user also wants the same, the application should keep them up to date about the events happened in the world. So, any kind of breaking news or latest updates of the market can be notified to the user with the help of Push Notifications.

How to implement Push Notifications?

Follow the below steps to implement Push Notification for an iOS Application.

  • Create a Simple XCode Application.
iOS Tutorials: Create a Single View App.
Create a Single View App.
  • Give name to the Application, and choose the desired team and identifiers.
iOS Tutorials: Give App Name, choose Team and Identifiers.
Give App Name, choose Team and Identifiers.
  • Save the project to the desired location.
iOS Tutorials: Save Project to desired location.
Save Project to the desired location.
  • Once the Application is created, navigate to the capabilities section.
iOS Tutorials: Navigate to Capabilities Section.
Navigate to Capabilities Section.
  • Toggle the Push Notification switch to “ON“, there will be a small window asking to select the, please select the desired team from the drop-down.
Toggle Push Notification Settings
Toggle Push Notification Settings

Note: Before getting into coding we need to make sure we have valid App Certificates, Push Notification Certificates, Valid Provisioning Profile. a .pem file is also required in case of a PHP based back-end server. A .p12 certificate would do for Firebase Cloud Messaging and Microsoft Azure-based Push Notifications.

Please visit the link and follow steps for creating valid App Certificates, Push Notification Certificates, Valid Provisioning Profile and .pem file.

  • Navigate to AppDelegate.swift file.
  • Add the below snippet of code in the file.
Code Snippet in AppDelegate File
Code Snippet in AppDelegate File
  • Call the method “registerForPushNotifications()“, in the didFinishLaunchingWithOptions method.
iOS Tutorials: Call regsiterForPushNotifications Method
Call regsiterForPushNotifications Method in didFinishWithLaunchingOptions.
  • Please select a valid team and Provisioning Profile.
iOS Tutorials : Please Select Valid Team and Provisioning Profile
Please Select Valid Team and Provisioning Profile
  • Please select the desired device (Physical device) and run the application (Command + R).
iOS Tutorials: Please select a physical device.
Please select a physical device.

Note: Here I am using Mac’s default application QuickTime Player so that we can show you how the device is behaving. After we run the application, there will be an alert asking for permission to Register for Push Notifications. Please click on “Allow“.

iOS Tutorials: Click on Allow
Click on “Allow” to receive a Valid device Token
  • A Device Token will be received. This Device Token is the key which will be used for sending Push Notifications to the user. Keep this Device Token For further use.
iOS Tutorials: Received Device Token
Received Device Token.

Note: Here we will be using a php based script to test Push Notifications. For sending Push Notifications via PHP Script, we need a .pem file.

To generate a .pem file follow the below steps:

  • Download the .cer file from the apple developer console.
iOS Tutorials: Download .cer file
Download .cer file from Apple Developer Console.
  • Install the .cer file on your Mac device and export the .p12 file from keychain access.
iOS Tutorials: Export Push Notification Certificates
Export Push Notification Certificates
  • Name the certificates and save to the desired location.
iOS Tutorials: Save Certificates
Save Certificates
  • Set password for the certificate.
iOS Tutorials: Certificate passwords
Certificate passwords
  • Open Terminal App on your Mac device and change your current directory to the folder where the .p12 file was saved.
iOS Tutorials: Generate .pem file
Generate .pem file
  • Enter this command in the terminal.
openssl pkcs12 -in <<certificate_fileName>>.p12 -out <<output_file_name>>.pem -nodes -clcerts
iOS Tutorials: Generate .pem file
Generate .pem file
  • .pem file generated successfully.
iOS Tutorials: .pem file Generated Successfully
.pem file Generated Successfully

You will have to download a XAMPP server, it can be downloaded from here.

Once you are done with the installation, on launching the Application it will look something like this.

iOS Tutorials: XAMPP Server Installation
XAMPP Server Installation
  • Open the application folder.
iOS Tutorials: XAMPP Server Installation
XAMPP Server Installation
  • Once done with opening the Application folder, navigate to htdocs folder and there add a new folder named as PushTest, copy paste the .pem file into that location.
  • Copy the below code and save it into a file named as push_apns.php and save it to the same location
    • if you see in the below code there is a variable $deviceToken ; make sure your device token is copied here.
    • pushcert.pem in the below file should be the name of your .pem file.


$deviceToken = ‘b855b3ef7ee85482ad40e5c1bc6437f4fd159cf5a736aa3cdd07466eb7b42659’;

// Put your private key’s passphrase here:
$passphrase = ‘123456’;

// Put your alert message here:
$message = ‘A push notification has been sent!’;

$ctx = stream_context_create();
stream_context_set_option($ctx, ‘ssl’, ‘local_cert’, ‘pushcert.pem’);
stream_context_set_option($ctx, ‘ssl’, ‘passphrase’, $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(‘ssl://gateway.sandbox.push.apple.com:2195’, $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp){
exit(“Failed to connect: $err $errstr” . PHP_EOL);


echo ‘Connected to APNS’ . “<br/>”;

// Create the payload body
$body[‘aps’] = array(
‘alert’ => array(
‘body’ => $message,
‘type’ => ‘sky_load’,
‘badge’ => 1,
‘sound’ => ‘default’,
‘mutable-content’ => ‘1’,
‘image_url’ => ‘image-url’,

// Encode the payload as JSON
$payload = json_encode($body);


// Build the binary notification
$msg = chr(0) . pack(‘n’, 32) . pack(‘H*’, $deviceToken) . pack(‘n’, strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
echo ‘Message not delivered’ . PHP_EOL;
echo ‘Message successfully delivered’ . PHP_EOL;

// Close the connection to the server

Once done with this process just execute this line in the browser.



Note: Make sure the application is in background. You can see the below output.

iOS Tutorials: Push Notifications Output
Push Notifications Output

We hope you liked the article!!.

Author: Chandra.rao

iOS Developer. #Life of an developer.... Learn New technology, Code, Learn New Technology, Code, Death.