I am using a PHP script to push messages onto android devices via GCM. I have an html form that takes in a text input as the message and then pushes this message to the android device.
Now when i tried it with two separate fields 1. Excerpt 2. Message My android device receives a null message.
I need to know where am I going wrong.
This is my HTML Form
<form id="<?php echo $row["id"] ?>" name="" method="post" onsubmit="return sendPushNotification('<?php echo $row["id"] ?>')">
<label>User:</label> <span><?php echo $row["id"] ?></span>
<div class="clear"></div>
<div class="send_container">
<textarea rows="3" name="excerpt" cols="10" class="txt_excerpt" placeholder="Type excerpt here"></textarea>
<textarea rows="3" name="message" cols="25" class="txt_message" placeholder="Type message here"></textarea>
<input type="hidden" name="regId" value="<?php echo $row["gcm_regid"] ?>"/>
<input type="submit" class="send_btn" value="Send" onclick=""/>
</div>
</form>
This is the ajax Script
<script type="text/javascript">
$(document).ready(function(){
});
function sendPushNotification(id){
var data = $('form#'+id).serialize();
$('form#'+id).unbind('submit');
$.ajax({
url: "send_message.php",
type: 'GET',
data: data,
beforeSend: function() {
},
success: function(data, textStatus, xhr) {
$('.txt_message').val("");
$('.txt_excerpt').val("");
},
error: function(xhr, textStatus, errorThrown) {
}
});
return false;
}
</script>
This is the Send_message.php code
if (isset($_GET["regId"]) && isset($_GET["message"]) && isset($_GET["excerpt"])) {
$regId = $_GET["regId"];
$message = $_GET["message"];
$excerpt = $_GET["excerpt"];
include_once './GCM.php';
$gcm = new GCM();
$registatoin_ids = array($regId);
$message = array("news" => $message, "exc" => $excerpt);
$result = $gcm->send_notification($registatoin_ids, $message);
echo $result;
This is the GCM.php Code
public function send_notification($registatoin_ids, $message, $excerpt) {
// include config
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'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);
// 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;
}
This is the android GCMIntentService Code
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("news");
String excerpt = intent.getExtras().getString("exc");
Log.i("Received ->", excerpt+" "+message);
if(message!=null)
{
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
}
All I see on my device is a blank notification whenever I push a message from the server.