【发布时间】:2015-02-15 16:11:23
【问题描述】:
大家好,我在我的应用中使用 Google 云消息传递。我正在关注我的应用程序的 android hive 教程。下面给出了教程的链接。
对我来说一切正常,但问题是当从远程服务器发送消息时,在客户端我收到消息警报,但没有消息只是写入空消息。我收到空消息。我该如何解决?
消息接收的 Java 代码
/** * 接收新消息时调用的方法 * */
@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);
}
服务器端代码
//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;
}
}
消息数组
$message = array("message" => $message);
提前致谢。
【问题讨论】:
标签: android google-cloud-messaging