2012-02-18 84 views
1

我已經實現了從C2DM服務器請求註冊ID的C2DM客戶端。一旦應用程序獲得註冊ID,應用程序將設備ID和註冊ID存儲在服務器數據庫中我已經實現了一個PHP服務器,但是當我嘗試發佈消息時,我得到Error = MissingRegistration。Android C2DM缺少註冊錯誤?

PHP服務器代碼

$url = 'https://www.google.com/accounts/ClientLogin'; 

$body = '[email protected]&Passwd=password&accountType=GOOGLE&source=MyLittleExample&service=ac2dm'; 
$c = curl_init ($url); 
curl_setopt ($c, CURLOPT_POST, true); 
curl_setopt ($c, CURLOPT_POSTFIELDS, $body); 
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true); 
$page = curl_exec ($c); 
$p = explode("Auth=",$page); 
curl_close ($c); 

$url = 'https://android.apis.google.com/c2dm/send'; 

$param = // Get Registration id from my sql 
$mtype = 'important'; 
$msg = 'Hello'; 

$len = strlen($param); 
echo $len; 
function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText) { 

     $headers = array('Content-length:300','Authorization: GoogleLogin auth='.$authCode); 
     $data = array(
      'registration_id' => $deviceRegistrationId, 
      'collapse_key' => $msgType, 
      'data.message' => $messageText //TODO Add more params with just simple data instead   
     ); 

     $ch = curl_init(); 

     curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send"); 
     if ($headers) 
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 


     $response = curl_exec($ch); 
     curl_close($ch); 
     return $response; 
    } 
$data = sendMessageToPhone($p[1],$param,$mtype,$msg); 
var_dump($data); 

根據該文件錯誤= MissingRegistration如果在發佈採購信息不registration_id參數發生。我甚至試圖通過對註冊ID進行硬編碼來運行應用程序。任何幫助不勝感激。謝謝。

+0

結構清晰,明確的問題。我甚至不知道PHP,但我仍然可以理解一切。值得+1。仍然在尋找問題。 – 2012-02-18 10:04:39

+0

@Boris謝謝:) – iNan 2012-02-18 10:06:47

回答

1

啊哈!這裏提供了可能的解決方案(從關於來自另一種我不知道的語言的通知的線索中獲取):https://stackoverflow.com/a/8339611/1108032。請閱讀他所說的解決他的問題。對我來說,看起來像這樣的應該會讓你失望,因爲代碼上你的代碼似乎沒問題。

+0

@謝謝你,bt仍然在努力:( – iNan 2012-02-18 10:27:08

+0

你從這篇文章嘗試了什麼?你有沒有嘗試再次註冊你的設備,以便刷新註冊ID? – 2012-02-18 10:44:00

+0

你試過,其實我試過後發現了錯誤,即如果我在curl中設置標題,post參數不會發送到服務器。再次感謝:) – iNan 2012-02-18 10:56:37