I'm an android developer and currently working on GCM server side using PHP. From my client side I'm storing the gcm response token in one database, and now I want to send message to the registered users, at one go. I'm not able to figure out how to do that, and I'm totally confused. My simple HTML file is:
<html>
<head>
<title>
GCM
</title>
</head>
<body topmargin="50" leftmargin="50" CLASS = "bg">
<form action="send_message.php" name="myform" method="post">
<h4>Admin for sending notificaion to the registered user</h4>
Notification Text: <input name="appText" size="15" type="text"/><br>
URL: <input name="url" size="15" type="text" /><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
And, my send_message.php:
<?php
error_reporting (0);
$link = mysql_connect("localhost","db","db");
if (!$link)
{
die(mysql_error()) ;
}
mysql_select_db("gcm_user")or die(mysql_error());
........................................
.....................................
.....................................
?>
Now, in the php file what should I do, I have tried this link Android Hive example
Just, now came through this link. Please suggest me how to use this solution for my problem. New link for my problem.
But I want only one php for my purpose. Please help in overcoming this prm.
Answering my own question. Here is what I have done to meet my requirements.
$gcm_text = $_POST['gcmText'];
$gcm_url = $_POST['gcmURL'];
$gcm_subText = $_POST['gcm_secondText'];
$sql = "select gcm_response_token as regId from GCM";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$gcm_array[]=$row['regId'];
$counter++;
}
mysql_free_result($result);
$body['allsearch'] = array(
'gcmText' => $gcm_text,
'gcm_secondText' => $gcm_subText,
'gcmURL' => $gcm_url
);
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $gcm_array,
'data' => $body,
);
//echo GOOGLE_API_KEY;
$headers = array(
'Authorization: key=$apiKey',
'Content-Type: application/json'
);
print_r($headers);
// 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);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
?>
By this, I was able to send json data to the client side and get the notification.
Those links seem to make the PHP over complicated. All you want to do is fill that $registrationIDs array with values from a query. I'm no PHP expert, but I used the often quoted CURL example and prepared statements like:
$ttl = 86400;
$randomNum=rand(10,100);
$registrationIDs = array();
.....
$queryregid = "select regid from registrations where YOUR SPECIFIC CRITERIA......";
if (!($stmt = $mysqli->prepare( $queryregid))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; }
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; }
if (!$stmt->bind_result($dev_i)){
echo "bind failed: (" . $stmt->errno . ") " . $stmt->error; }
/* fetch values */
while ($stmt->fetch()) {
$regidOne = $dev_i;
$registrationIDs[] = $regidOne; // Fill the array
}
$stmt->close();
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message),
'delay_while_idle'=> false,
'time_to_live' => $ttl,
'collapse_key'=>"".$randomNum.""
);
$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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
.