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
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
Post a Comment