Skip to main content

Apple Push Notification service (APNs) - for notifying iphone app users about latest and interesting things happening even if they are not using App

Apple Push Notification service

There are thousands of applications available for iphone and android devices. Here we will be discussing about the Apple Push Notification service which helps to alert user of an iphone application about new data from Application provider. Suppose an Online magazine website is providing an iphone app to it users, where they can browse their magazine and download it their iphone. When a new online edition is uploaded, the website will trigger the Notification service to send message / notification to its iphone app users related to availablity of new edition. So that they are informed about latest updates on the site.

Apple Push Notification service transports and routes a notification from a given provider to a given device. Here what happens is , when an app is installed on iphone, it asks whether user wish to get push notifications, If user confirms then a device token is generated for that device and it is send to the app provider's server and there the device token is saved for future notify process. When a new or interesting thing happens on the site, the Service provider (may be a Website) send notification to Apple Push Notification Server (APNS). A notification is a short message consisting of two major pieces of data: the device token and the payload. The device token is analogous to a phone number; it contains information that enables APNs to locate the device on which the client application is installed. The server identifies the device and send notification to the device (iphone) and the device passes the notification to the installed App. The payload is a JSON-defined property list that specifies how the user of an application on a device is to be alerted.

There are three things a push notification can do ( This is passed to APNs as JSON payload):
Display a text message as an alert
Play a brief sound
Set a number in a badge on the app’s icon

When the user’s device receives the push notification, it shows an alert, plays a sound and/or updates the app’s icon. The user can launch the app from the alert. The contents of the push notification can be passed to the app where it can be used as needed.

On the Providers website You can use a server side script like PHP to connect to Apple Push Notification Server and send the notification data having device token and Payload.

Sample PHP script to connect o APN server


$apns_link="ssl://gateway.push.apple.com:2195"; //live apn server
//$apns_link="ssl://gateway.sandbox.push.apple.com:2195"; //test apn server

$pem_file="cp.pem";
$passphrase="YOUR_PASSPHRASE";
$deviceToken="740d89jmbebcf74f9b7c25d48e335894f5f6aa01da5hddb387462c7reaf61bb78ad";
$message="New Magazine uploaded, please check";

$ctx = stream_context_create();

//stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'local_cert', $pem_file );

stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client($apns_link, $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

// checkinging for connection establishment
if (!$fp)
exit("Failed to connect to APN Server: $err $errstr" . PHP_EOL);

echo 'Connected to APN Server.' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
'alert' => $message,
'badge' => 1,
'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;

fclose($fp);


for more info, please refer

APPLE PUSH NOTIFICATION SERVICE VIDEO TUTORIALS

www.developer.apple.com
www.raywenderlich.com

Comments

Popular posts from this blog

Strange problem occured while trying to create a CSV file using PHP Script - The file is not seen on FTP but can download using file's absolute path url

Strange problem occured while trying to create a CSV file - The file is not seen on FTP but can download using file's absolute path url Last day I came across a strange problem when I tried to create a csv file on therver using a PHP script. the script was simply writing a given content as a csv file. The file will be created runtime. What happened was, The script executed fine, file handler for new file was created and contents was wrote into the file using fwrite and it returned the number of bytes that was written.

How to get the Query string of a URL using the Javascript (JS)?

JS function get the Query string of a URL or value of each parameter using the Javascript(JS)? If you want to get your current page's url var my_url=document.location; to get the query string part of the url use like this: var my_qry_str= location.search; this will return the part of the url starting from "?" following by query string Lets assume that your current page url is http://www.crozoom.com/2013/page.html?qry1=A&qry2=B then the location.search function will return " ?qry1=A&qry2=B " to exclue "?", do like this:


Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »
Member
Search This Blog