没有从Android应用程序上的GCM引擎得到任何响应

时间:2015-06-20 12:31:34

标签: android google-cloud-messaging

我做了chat application using GCM。当我尝试将消息从一个设备发送到另一个设备时,我没有收到来自gcm_engine.php的任何回复。服务器正常运行,因为应用程序识别所有其他文件,如save_reg_id.php等。请告诉我哪里出错了。 API_key,message and chattingToDeviceId已经过验证,并且是正确的。 这是我的sendmessage函数:

public void sendMessage() {

    final String messageToSend = edtMessage.getText().toString().trim();

    if (messageToSend.length() > 0) {

        Log.i(TAG, "sendMessage");

        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    httpclient = new DefaultHttpClient();
                    httppost = new HttpPost(utils.getCurrentIPAddress2());//returns the path to 'gcm_engine.php'

                    nameValuePairs = new ArrayList<NameValuePair>(1);
                    nameValuePairs.add(new BasicNameValuePair("message",
                            messageToSend));

                    nameValuePairs.add(new BasicNameValuePair(
                            "registrationIDs", chattingToDeviceID));

                    nameValuePairs.add(new BasicNameValuePair("apiKey",
                            API_KEY));

                    httppost.setEntity(new UrlEncodedFormEntity(
                            nameValuePairs));
                    //ResponseHandler<String> responseHandler = new BasicResponseHandler();
                    //final String response = httpclient.execute(httppost,
                        //  responseHandler);
                    response = httpclient.execute(httppost);
                    String responseBody = EntityUtils.toString(response.getEntity());
                    utils.showToast("reached end-"+responseBody);//returns "reached end-" while it should return responseBody also
                    Log.i(TAG, "Response : " + responseBody);
                    if (responseBody.trim().isEmpty()) {
                        Log.d(TAG, "Message Not Sent");
                    }
                    else
                        utils.showToast("msg sent");

                } catch (Exception e) {
                    //utils.showToast("exception-"+e.getMessage());
                    Log.d(TAG, "Exception : " + e.getMessage());
                }
            }
        };

        thread.start();

    }

}

以下是gcm_engine.php

<?php

// Message to be sent
 //$message = "DEMO MESSAGE";
// $registrationIDs = "APA91bF_W_zIg-T9RInVmgwLM0OkaQNsjrbJ1u9Wa6-nTEVxDqobw-YWHqFWJB1_kxgINO9RMrEJBIvd_6sSJIh7aEQyMs1THaSOFuzt9CF2eFZU9zAnqsEubYrCcRmQrFu4vv54mIkGwitIP_X_7SYkM0rMgVQWwe5z2sWqfDdspJo49vtayIY";
// $apiKey = "AIzaSyCBI_c2izVrEPcJ509uZGVLdfWUAW-rg48";

$message = $_POST['message'];
$registrationIDs = $_POST['registrationIDs'];
$apiKey = $_POST['apiKey'];

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

$fields = array(
            'registration_ids'  => array( $registrationIDs ),
            'data'              => array( "message" => $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 );

curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

// Execute post
$result = curl_exec($ch);
 echo $result;
// Close connection
curl_close($ch);



?>

1 个答案:

答案 0 :(得分:0)

问题解决了!我在gcm_engine.php

中遗漏了这些行
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);   
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);