Push notification in iOS

Recently i had a task where i had to implement the push notification for both android and iPhone. While i was pretty new to using push notifications or even have an idea about that, i found that there were very few source available online that really helped my cause. After going back and forth with the different sets of codes that i got, i finally succeeded in implementing the push notification. I would like to share it here :

Class that links with the apple server :

class notificationClass
	private $pemFileName;
	private $sslPath;
	// Send Notification to iPhone Mobiles
	public function iPhoneSendNotification($message,$deviceToken)
		  $badge   = 7;
		  $body = array();
		  $body['aps'] = array('alert' => $message);
		  if ($badge)
		  $body['aps']['badge'] = $badge;
		  $ctx = stream_context_create();
		  // Please add the comment
		  stream_context_set_option($ctx, 'ssl', 'local_cert', '../../certificates/apns-dev.pem');
		  $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
		  if (!$fp) {
		   print "Failed to connect $err $errstr\n";
		   //return false;
		  else {
		   //print "Connection OK\n";
		   $payload = json_encode($body);
		   $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
		   print "sending message :" . $payload . "\n";
		   fwrite($fp, $msg);
		   return true;

To explain more, basically we need the following things for implementing push notification :

1. PEM file (development/distribution certificate)
2. Device Token(unique for each app installation for a particular device)
3. Message(256 bytes max)
4. Badge(not compulsory)

PEM File

PEM file – This is basically a certificate that is required for any activity related to iOS and apple developer account. Detailed steps are provided here on how to generate a PEM file

Device Token – Device token is a long alphanumeric character set which is unique for each app installation. Note that device token and device UDID are entirely different. Device token will change with every app while udid will be unique for each device.

You will get it in your app delegate if registration is successful in method

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

Or you wont if it fails, with a call to this method:

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

There are more details in the docs: https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html

Message – Message is what you actually send to the device as notification. It has a character limit of 256 bytes.

Badge – Badge is actually a number will be displayed as the notification. Explaining literally, the badge is the little red circle on the top left corner of the app button with a number in it. Like the email or text message button telling u how many unread messages u have.

Now that you have all the parameters and certificates, you can now easily implement the push notification by initiating an instance of class as follows :

//Push Notification iOS
$iOS = new notificationClass();
//Push Notification iOS
$notificationText = "Sample push notification";
$deviceToken = "ASDASFSDFW#$%SFDFSDFSDFSDF";//dummy
$iOS->iPhoneSendNotification($notificationText, $deviceToken);

Push notification for Android is explained in the next post.

Leave a Reply

Your email address will not be published. Required fields are marked *