Android GCM提供Null消息 [英] Android GCM gives Null message
问题描述
你好我在应用程序中使用Google云消息的每一个人。我在下面给出了我的app.link的android hive教程。
$ b http://www.androidhive.info/2012/10/android-push-notifications -using-google-cloud-messaging-gcm-php-and-mysql /
所有工作都适合我,但问题在于,从远程服务器发送,在客户端我收到消息警报,但没有消息只写了空消息。我收到空消息。如何修复它?
消息接收的Java代码
/ **
*方法呼吁接收新消息
* * /
@Override
protected void onMessage(Context context,意图意图){
Log.i(TAG,收到的消息);
String message = intent.getExtras()。getString(price);
displayMessage(context,message);
//通知用户
generateNotification(context,message);
}
服务器端代码
//将代码放在这里
//构造函数
函数__construct(){
$ b / **
*发送推送通知
* /
public function send_notification($ registatoin_ids,$ message){
// include config
include_once($ _ SERVER [DOCUMENT_ROOT]。*****);
$ apiKey =*******;
//设置POST变量
$ url ='https://android.googleapis.com/gcm/send';
$ fields = array(
'registration_ids'=> $ registatoin_ids,
'data'=> $ message,
);
$ headers = array(
'Authorization:key ='。$ apiKey,
'Content-Type:application / json'
);
//打开连接
$ ch = curl_init();
//设置网址,POST变量的数量,POST数据
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
//临时禁用SSL证书支持
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ ch,CURLOPT_POSTFIELDS,json_encode($ fields));
//执行后
$结果= curl_exec($ ch);
// if($ result == FALSE){
// die('Curl failed:'。curl_error($ ch));
//}
//关闭连接
curl_close($ ch);
echo $ result;
}
}
数组消息
$ message = array(message=&$; $ message);
在此先感谢。
它是一个猜测,确保从服务器发送的键
与您在客户端使用的相同。在本教程中,它的价格
。所以确保两者都是相同的。
服务器端: Android: $ message = array(price=> $消息);
Log.i(TAG,Received message);
String message = intent.getExtras()。getString(price);
displayMessage(context,message);
//通知用户
generateNotification(context,message);
}
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.
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.
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);
}
这篇关于Android GCM提供Null消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!