How to implement and test push notification in Cordova for iOS using local PHP server

Posted By : Akhil Dhiman | 25-Mar-2015

Here I will show you how one can setup push notification to ios application.

Plugin to use:

I use the below plugin to enable push notification to device.

https://github.com/phonegap-build/PushPlugin

 

To add this plugin open terminal and execute below command:

 

 cordova plugin add https://github.com/phonegap-build/PushPlugin.git
 

How to setup push notification in device:

To enable push notification in device you need to write the following code snippet into device ready event.

 document.addEventListener('deviceready', registerNotification, false);
function registerNotification() {
    var pushNotification;
    try {
        pushNotification = window.plugins.pushNotification;
        pushNotification.unregister(function() {
             //console.log("Notification Unregistered");
        }, function() {
             //console.log("Error in unregistering notification");
        });
        pushNotification.register(tokenHandler, errorHandler, {
	     "badge" : "true",
	     "sound" : "true",
	     "alert" : "true",
	     "ecb" : "onNotificationAPN"
        });
        // required!

    } catch(err) {
        txt = "There was an error on this page.\n\n";
        txt += "Error description: " + err.message + "\n\n";
        alert(txt);
    }

    function tokenHandler(result) {
        alert("token:      "+ result);
        console.log("token:      "+ result);
        // Your iOS push server needs to know the token before it can push to this device
        // here is where you might want to send it the token for later use.
    }

    function successHandler(result) {
        alert(result);
    }

    function errorHandler(error) {
        alert(error);
    }
};

// handle APNS notifications for iOS
function onNotificationAPN(event) {
    if (event.alert) {
            //navigator.notification.alert(event.alert);
    }
    if (event.sound) {
            var snd = new Media(event.sound);
            snd.play();
    }
    if (event.badge) {
            
    }
}
 

Once you setup with plugin and code then do the following :

  • Request a Certificate from a Certificate Authority.

Open Keychain access.

  • Open certificate assistance and request a certificate.

  • Fill your email and certificate name as shown in picture save certificate file.
  • Go to keys tab and find your certificate and export your certificate as shown in image.

  • Save the private key as pushnotification.p12

Making the App ID and SSL Certificate

Open developer account with user credentials.

  • Create App ID.
  • Download Certificate file with aps_development.cer name.
  • Create Provisional profile with package name.
  • Download Provisional Profile.

Now if you have a server you can just send the export p12 file to the server. and provide device token to server. If you dont have server then generate a PEM file.

Making a PEM File

So now we have three files:

  1. The CSR
  2. The private key as a p12 file (pushnotification.p12)
  3. The SSL certificate, aps_development.cer

Store the three files in safe location so that we can use them in future.

Now our first step is to convert .cer file into a .pem file to convert this open terminal and write below command.

 openssl x509 -in aps_development.cer -inform der -out pushnotificationCert.pem
 

Now we need to convert the private key’s .p12 file into a .pem file, execute the below commamds.

 openssl pkcs12 -nocerts -out pushnotificationKey.pem -in pushnotification.p12
Enter Import Password: 
MAC verified OK
Enter PEM pass phrase: 
Verifying - Enter PEM pass phrase:
 

Now we need to convert both cert.pem and key.pem file into one .pem file.

 cat pushnotificationCert.pem pushnotificationKey.pem > ck.pem
 

If you want to check weather your certificate work or now execute the following line:

 telnet gateway.sandbox.push.apple.com 2195
Trying 17.172.232.226...
Connected to gateway.sandbox.push-apple.com.akadns.net.
Escape character is '^]'.
 

This will create a reguiler connection to APNS. Now if the above response comes then terminate the command and execute another command.

 openssl s_client -connect gateway.sandbox.push.apple.com:2195 
-cert pushnotificationCert.pem -key pushnotificationKey.pem
Enter pass phrase for pushnotificationKey.pem:
 

Make PHP server to test push notification

Please copy the below code snippet to a file and save it with name pushnotification.php

  $message,
	'sound' => 'default'
	);

// 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;
else
	echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);
 

You just need to add device token, password and message to send push notification.

After then copy pushnotification.php and ck.pem file in one directory and run following command.

 php pushnotification.php
 

A push notification has been received on your device.

 

THANKS

About Author

Author Image
Akhil Dhiman

Akhil is an iPhone and Android application developer with experience in PhoneGap and Swift(Native iOS). Akhil has good experience working with JavaScript, jQuery and Underscore as well.

Request for Proposal

Name is required

Comment is required

Sending message..