DEV Community

Android value
Android value

Posted on

Build Push Notification Service

If you are going to use this for your own application, then I recommend building your own API interface with the APNS server. The component would be as follows.

1.Your phone.
2.Your server.
3.Apple Push Notification Service (APNS)

First you need the iOS application to register for receiving push notifications. Then as per the documentations, create the .p12 file and then convert it into the PEM file. After that part has been complete, you have to build your own interface with the APNS server.

Apple have now released their enhanced notification system, which you can use to now track individual notifications that is being sent out from your server. The forensic breakdown of the APNS interface in PHP would be like follows.

First you need to build the payload with the following anatomy.

$payload='{

"aps": {

"alert":{

"title": "'.$notif_desc.'",

"body":"'.$notif_title.'" },

"badge":"'.$badge.'",

"priority":"'.$priority.'",

"sound": "default"

},

"type": "notification",

"id":"'.$lastid.'",

"date":"'.$date1.'"

}';

//The aps tagname in JSON is mandatory.

//set connection parameters.

$ctx = stream_context_create();

stream_context_set_option($ctx, 'ssl', 'local_cert', 'pushcert.pem');

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

// Open a connection to the APNS server

$fp = stream_socket_client(

'', $err,

$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

stream_set_blocking ($fp, 0);

if (!$fp)

{

exit("Failed to connect: $err $errstr" . PHP_EOL); }

else{

//echo $fp;

echo 'Connected to APNS' . PHP_EOL;

$apple_expiry = time() + (90 * 24 * 60 * 60);

Now call the APNS server inside a loop, which iterates through all the values in the device token array.

// Loop start

$msg = pack("C", 1) . pack("N", $apple_identifier) . pack("N", $apple_expiry) . pack("n", 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n", strlen($payload)) . $payload;

// SEND PUSH

$writestat=fwrite($fp, $msg);

//Loop Ends

This is a simple PHP server side code which I have used in my application to push to APNS server.You can use it as a reference.

Top comments (0)