<?php
$host = 'localhost'; // most of the time
$user = '';
$pass = '';
$database = '';
$connection = mysql_connect ($host, $user, $pass) or die('Error connecting to mysql'.mysql_error());
mysql_selectdb($database) or die ('->>Error selecting database'.mysql_error());
$res=mysql_query("SELECT devicetoken FROM devicetokensiphone");
$index1 = 0;
$num1=mysql_num_rows($res);
if($num1!=0){
while ($row1 = mysql_fetch_array($res, MYSQL_NUM))
{
$abc1[$index1] = $row1[0];
$index1++;
}
$registrationIDs1 = $abc1;
//print_r($registrationIDs1);exit;
}
//$deviceToken = '';
//$deviceToken = '';
$passphrase = '';
$today = date("m.d.y");
//$msg=$msg.$today;
$message = $_GET["msg"];
//$message=$message.$today;
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'Certificates.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// 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" . PHP_EOL);
//echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'body' => 'Push Notification'
);
// Encode the payload as JSON
$payload = json_encode($body);
foreach($registrationIDs1 as $value){
$msg = chr(0) . pack('n', 32) . pack('H*', $value) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
}
// 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));
fclose($fp);
//for andorid
/*$host = 'localhost'; // most of the time
$user = '';
$pass = '';
$database = '';
$connection = mysql_connect ($host, $user, $pass) or die('Error connecting to mysql'.mysql_error());
mysql_selectdb($database) or die ('->>Error selecting database'.mysql_error());*/
$result=mysql_query("SELECT devicetoken FROM devicetokens");
$index = 0;
$num=mysql_num_rows($result);
if($num!=0){
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
$abc[$index] = $row[0];
$index++;
}
// Replace with real server API key from Google APIs
$apiKey = "";
$registrationIDs = $abc;
//print_r($registrationIDs);
// Message to be sent
$message1 = $_GET["msg"];
//$message1=message1.
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
//$message1=$message1.$today;
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message1 ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
// echo $result;
//print_r($result);
//var_dump($result);
}
//end
$_SESSION["notify"]=1;
echo "<script language='javascript'>document.location.href='index.php?option=com_pushnotification'</script>";
?>