I need to send a notification to multiple devices via server but it didn't work for me,it works fine for one.So i tried to make a loop after getting all the gcm_regid
but it doesn't work.Here is my code : Function on main page :
function sendPushNotificationToAll(){
var data = $('#form').serialize();
$.ajax({
url: "send_message(all).php",
type: 'GET',
data: data,
beforeSend: function() {
},
success: function(data, textStatus, xhr) {
$('.txt_message').val("");
},
error: function(xhr, textStatus, errorThrown) {
}
});
return false;
}
and send_message(all).php :
include_once './config.php';
if (isset($_GET["message"])) {
$message = $_GET["message"];
mysql_connect("localhost","root","");
mysql_select_db("gcm");
$fetch=mysql_query("SELECT * FROM gcm_users");
$ids_array=array();
while($row = mysql_fetch_array($fetch))
{
$ids_array[]=$row['gcm_regid'];
}
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $ids_array,
data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
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_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
ie('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
When I click on send nothing happens.
update Ok so answering my own question : This code misses on line which is :
$message = array("price" => $message);
That's all!