问题
Hello every one I am using Google cloud messaging in my app. I am following android hive tutorial for my app.link of the tutorial is given below.
http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
All is working fine for me but the problem is that when a message is sent from remote server, on client side i receive alert of message but having no message just written null message. I am receiving null message. how can i fix it?
Java Code on Message Receive
/** * Method called on Receiving a new message * */
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
Server side code
//put your code here
// constructor
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// include config
include_once($_SERVER["DOCUMENT_ROOT"] ."*****");
$apiKey = "*******";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$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);
// 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;
}
}
array of mesage
$message = array("message" => $message);
Thanks in Advance.
回答1:
Its a guess, Make sure the key
which send from the server is same as you used in the client side. In this tutorial its price
. So make sure both are same.
Server side: $message = array("price" => $message);
Android:
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
回答2:
If you are sending a "message" parameter from the server :
$message = array("message" => $message);
You should change the client code from :
String message = intent.getExtras().getString("price");
to :
String message = intent.getExtras().getString("message");
This way the client code would read the parameter you sent to it from the server.
来源:https://stackoverflow.com/questions/27520054/android-gcm-gives-null-message