RSS

iPhone Push Notifications PHP Server

25 Jul

iPhone Push Notifications PHP Server

<?php
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', YOUR_.PEM_FILE);
stream_context_set_option($ctx, 'ssl', 'passphrase', YOUR_PASSPHRASE);

$fp = stream_socket_client(
        'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp) {
    exit("Failed to connect: $err $errstr<BR><BR>");
}

echo 'Connected to APN<BR>';


// Create the payload body
$body['aps'] = array(
    'badge' => +1,
    'alert' => 'Testing push notifications',
    'sound' => 'new_wall_paper.wav',
    'action-loc-key' => 'OK'
);

$payload = json_encode($body);

for ($i = 0; $i < count($tokens); $i++) {
    $msg = chr(0) . pack('n', 32) . pack('H*', $tokens [$i]) . pack('n', strlen($payload)) . $payload;
    fwrite($fp, $msg, strlen($msg));
}

echo "Completed sending messages";
fclose($fp);

Advertisements
 
1 Comment

Posted by on July 25, 2014 in iphone, PHP

 

Tags:

One response to “iPhone Push Notifications PHP Server

  1. Peter Bronze

    July 27, 2014 at 12:57 am

    Thanks for that piece of code, Rajitha.

    I’d like to share a link to http://quickblox.com/developers/SimpleSample-messages_users-ios – you can find more code samples there as well as use the backend part free of charge when developing your apps. Hope that helps.

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: