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 use WiFi adapter on Ubuntu 16.04 desktop PC - Realtek RTL8188EUS 802.11n Wireless USB Network Adapter Driver installation

Installation of Realtek RTL8188EUS 802.11n Wireless USB Network Adapter on Desktop PC having Ubuntu 16.04 OS My PC is running in Ubuntu 16.04 OS, recently I thought of using a dongle wife adapter to access our home's Wifi network. For this I used Realtek RTL8188EUS 802.11n Wireless USB Network Adapter ( Model No: OT-WUA950NM ) This small device cost around Rupees 250/- to Rs 300/- in India. I did the following steps for installation of this Realtek Nano Wifi Adapter: Plug Realtek RTL8188EUS 802.11n Wireless USB Network Adapter to your PC's USB port, Take the terminal application and run the command "lsusb" to list the plugged in usb devices: Myhome:~$ lsusb Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 001 Device 003: ID 0bda:8179 Realtek Semiconductor Corp. R...

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***


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