2011-08-30 11 views
4

我只是想用android推送通知(C2DM)來測試應用程序,我相信android代碼沒有任何問題。我使用PHP代碼連接到推送服務器併發送消息,其在本地服務器中正常工作。Android推送通知PHP代碼不能在第三方服務器上工作,在本地服務器上工作

但是,如果我在第三方服務器運行相同,它沒有運行。它不給我任何錯誤信息。並且它說「HTTP狀態200 OK」作爲響應。

供您參考我在添加代碼,我從一些網站得到了這個。

<?php  
    $username ="[email protected]"; 
    $password = "password"; 
    $source="My-Server-Event-Alerter"; //anything that says about ur app 
    $service="ac2dm"; 

    // local server 
    $message =$_GET ['message']; 

    if ($_GET ['message'] != '') 
    { 
     $message =$_GET ['message']; 
     echo 'Message sent to server '.$message; 

     $post_params = array( 
         "Email" => $username, 
         "Passwd" => $password, 
         "accountType"=>"HOSTED_OR_GOOGLE", 
         "source" => $source, 
         "service"=>$service 
         ); 

     $first = true; 
     $data_msg = ""; 

     foreach ($post_params as $key => $value) 
     { 

      if ($first) 
       $first = false; 
      else 
       $data_msg .= "&"; 

      $data_msg .= urlencode($key) ."=". urlencode($value); 
     } 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin"); 

     $data = array(
        'accountType' => HOSTED_OR_GOOGLE, 
        'Email' => $username, 
        'Passwd' => $password, 
        'source'=> $source, 
        'service'=>$service 
        ); 

     curl_setopt($ch, CURLOPT_HEADER, true); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); 
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

     // for debugging the request 
     //curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging 
     $response = curl_exec($ch); 

     curl_close($ch); 
     echo $response; 
     $pos = strpos($response, "Auth="); 
     $authKey = trim(substr($response, 5+$pos)); 
     echo $authKey; 

     echo 'Device Token: '. $deviceToken . ''; 
     $data = array( 
     'registration_id' => $deviceToken, 
     'collapse_key' => 'ck_' . 'col_key', 
     'data.message' => $message, 
     'data.title' =>'Request Push Demo' 
     ); 

     //$data = (is_array($data)) ? http_build_query($data) : $data; 

     $ch = curl_init(); 

     curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send"); 
     echo 'Content-Length:'.strlen($data); 
     $headers = array('Authorization: GoogleLogin auth=' . $authKey,'Content-Length:'.strlen($data)); 
     if($headers) 
     { 
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     } 

     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
     curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
     $messagedata = curl_exec($ch); 
     //var_dump(curl_getinfo($ch)); //for debugging the request 
     //var_dump($messagedata); 
     // echo $messagedata; 
     curl_close($ch); 
    } 
?> 



<html> 
<form action ="./c2dmtest2.php" method="get" enctype="application/x-www-form-urlencoded" name="Send Notification" target="_self"> 
<label>Push Message 
<input name="message" type="text" size="50" maxlength="50"></label> 
<input name="Send Message" type="submit" value="Send Message"> 
</form> 
</html> 
+0

你檢驗兩臺服務器已經安裝了相同的擴展?特別是CURL?是否存在無效或不可信的SSL連接問題? 您可能必須將CURL設置爲接受任何SSL證書並且不驗證它。 – ToBe

+0

你是否從curl_error()得到任何CURL消息? – ToBe

+0

嘗試GCM.time已更改。 – Notepad

回答

0

你可以添加代碼的頂部以下行,才能看到潛在錯誤:

error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
相關問題