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

How to delete videos from your Youtube Watch History list?

How to Delete Individual or all videos from your Youtube Watch History list? Youtube keeps a fine record of the videos that you had watched earlier. You can view this by visiting the History section. If you want to remove the video's from the list do the following: Logon to Youtube and click on the "History" tab on the left menu to view Watch History ( Read more ) There will be check boxes corresponding to each video in the list Tick the check boxes of the videos which you want to remove Click on " Remove " button to delete the videos.

ICICI prudential Customer portal updated - Option to change password is missing - Know how to change your ICICI prudential password

Recently I received an SMS from ICICI prudential asking for login to their website's customer portal using the phone number as user Id and an autogenerated one time password given in the message as password. The SMS messsage was like this. Dear ***Cust Name*** login to your policy(ies) on www.iciciprulife.com with your user id as **mobile number*** and One time use password as ***password***

What are the Income Tax Rates for Indian citizens for Financial Year 2017-2018?

Income Tax Slab and Rates given below are for Indian citizens of age less than 60. This rates are applicable for the Financial Year 2017-2018 Income Tax Slab Rates Financial Year 2017-2018 Assessment Year 2018-19 Income Tax Slab Rates SLAB 1 Individuals whose total income not exceeding Rs. 2,50,000 ( 2.5 lakhs ) They are exempted from paying income tax.


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