2015-06-20 39 views
0

我做了一個chat application using GCM。當我嘗試從一臺設備發送消息到另一臺設備時,我沒有收到gcm_engine.php的任何響應。服務器運行良好,因爲該應用程序可以識別所有其他文件,如save_reg_id.php等。請告訴我哪裏出錯了。 API_key,message and chattingToDeviceId已驗證,他們是正確的。 這裏是我的sendmessage功能:沒有得到任何響應從GCM引擎對Android應用程序

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); 



?> 
+0

嘿這些線,我從來沒有使用GCM,所以我不知道,但你不應該保持apiKey/registrationIDs私有? –

+0

這些不是我的apiKey和RegID。這只是一個例子。 – user3319410

回答

0

問題解決了!我是缺少在gcm_engine.php

curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);