push notification on ios multiple device

Standard

<?php

require_once ‘config.php’;

$results = “SELECT * FROM `testuser_register` where sourcemedia=’iOS'”;
$result = mysqli_query($conn, $results);
//Setup notification message
$body = array();
$i=0;
while($row = mysqli_fetch_array($result))
{
$deviceToken=array();
$apple_identifier = $row[“id”];
$deviceToken[$i] = $row[“tokenid”];
$prefix = $fruitList = ”;
$comma_separated[$i] = implode(” ‘,’ “, $deviceToken);
$i++;
}

// set time limit to zero in order to avoid timeout
set_time_limit(0);

// charset header for output
header(‘content-type: text/html; charset: utf-8’);

// this is the pass phrase you defined when creating the key
$passphrase = ”;

// you can post a variable to this string or edit the message here
if (!isset($_POST[‘msg’])) {
$_POST[‘msg’] = “Notification message here!”;
}

// tr_to_utf function needed to fix the Turkish characters
$message = ‘Hi vinod sunil’;

/*$deviceToken =array();
$unitSql = “SELECT * FROM `testuser_register`”;
$query = mysqli_query($conn, $unitSql);

while($unitRes = mysqli_fetch_array($query)){

$deviceToken[] = $unitRes[“tokenid”];

}*/

$deviceIds=$comma_separated;

 

// load your device ids to an array
//$deviceIds = array(‘E805EFB0099BEB2F9B2471436D1495D11D2AE12057416EA8CAA2569C6591827C’, ‘C93A7204E24F4C4119C1CB3E7FE4206689B9F447DB7EB8AF940F19F9271F3E08’);
print_r($deviceIds);
// this is where you can customize your notification
$payload = ‘{“aps”:{“alert”:”‘ . $message . ‘”,”sound”:”default”}}’;

$result = ‘Start’ . ‘<br />’;

////////////////////////////////////////////////////////////////////////////////
// start to create connection

$ctx = stream_context_create();
stream_context_set_option($ctx, ‘ssl’, ‘local_cert’, ‘../pem/atmcDev.pem’);
stream_context_set_option($ctx, ‘ssl’, ‘passphrase’, $passphrase);

echo count($deviceIds) . ‘ devices will receive notifications.<br />’;

foreach ($deviceIds as $item) {
// wait for some time
sleep(1);

// Open a connection to the APNS server
$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 />’);
} else {
echo ‘Apple service is online. ‘ . ‘<br />’;
}

// Build the binary notification

$msg = chr(0) . pack(‘n’, 32) . pack(‘H*’, $item) . pack(‘n’, strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result) {
echo ‘Undelivered message count: ‘ . $item . ‘<br />’;
} else {
echo ‘Delivered message count: ‘ . $item . ‘<br />’;
}

if ($fp) {
fclose($fp);
echo ‘The connection has been closed by the client’ . ‘<br />’;
}
}

echo count($deviceIds) . ‘ devices have received notifications.<br />’;

// set time limit back to a normal value
set_time_limit(30);
?>

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s