Stuti Shah February 2016

GCM push notification coming but with empty message

When I submit data through php file it is giving notification to registered phone but there is an empty message only no message content.Mostly tried all tutorial available online but not getting it.Please help.

Server Side Code:php file

<?php
$con = mysql_connect("localhost", "umane", "pass");
if(!$con){
die('MySQL connection failed');
}

$db = mysql_select_db("dbname");
if(!$db){
die('Database selection failed');
}

$registatoin_ids = array();
$sql = "SELECT * FROM tblname";
$result = mysql_query($sql, $con);
while($row = mysql_fetch_assoc($result)){
array_push($registatoin_ids, $row['registration_id']);

}

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$message = array("Notice" => $_POST['message']);
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);

$headers = array(
'Authorization: key= MY 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;
?>

Android Code:

public class GcmSender {

    public static final String API_KEY = "My API KEY";

    public static void main(String[] args) {
        if (args.length < 1 || args.length > 2 || args[0] == null) {
            System.err.println("usage: ./gradlew run -Pmsg=\"MESSAGE\" [-Pto=\"DEVICE_TOKEN\"]");
            System.err.println("");
            System.err.println("Specify a test message to broadcast via GCM. If a dev        

Answers


TP89 February 2016

You are certain that it's retrieving the POST variable in your PHP code? Try to replace this:

$message = array("Notice" => $_POST['message']);

with:

$message = array("Notice" => "testing");

Also in your receiver you are saying

String message = data.getString("data");

But you defined your data as "Notice" in the php code, so to retrieve the data you must say:

String message = data.getString("Notice");

Post Status

Asked in February 2016
Viewed 1,835 times
Voted 8
Answered 1 times

Search




Leave an answer