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.
- Give name to the Application, and choose the desired team and identifiers.
- Save the project to the desired location.
- Once the Application is created, navigate to the 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.
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.
- Call the method “registerForPushNotifications()“, in the didFinishLaunchingWithOptions method.
- Please select a valid team and Provisioning Profile.
- Please select the desired device (Physical device) and run the application (Command + R).
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“.
- 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.
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.
- Install the .cer file on your Mac device and export the .p12 file from keychain access.
- Name the certificates and save to the desired location.
- Set password for the certificate.
- Open Terminal App on your Mac device and change your current directory to the folder where the .p12 file was saved.
- Enter this command in the terminal.
openssl pkcs12 -in <<certificate_fileName>>.p12 -out <<output_file_name>>.pem -nodes -clcerts
- .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.
- Open the application folder.
- 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);
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));
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.
We hope you liked the article!!.