2011-01-31 118 views
0

我正在開發使用C2DM的應用程序。我已收到註冊ID。我如何將它發送到服務器。任何人都可以用代碼解釋我嗎?的Android C2DM應用

+0

您好Vishwa我也在做一個工作,在Android的C2DM發送通知,但現在我無法得到registrationnid形式c2dm服務器,你在你的問題中提到你有一個註冊ID請提供獲取註冊ID的代碼。在此先感謝..請請幫助我.. – DynamicMind 2011-03-10 09:53:53

回答

1

這裏是一個PHP函數發送C2DM消息到手機,與註冊ID。

/** * 發送消息到手機。

  • @static
  • @access公共
  • @參數字符串$ AUTHCODE谷歌授權通過googleAuthenticate()
  • @參數字符串$ deviceRegistrationId裝置期間從谷歌獲得的設備註冊ID獲得該服務器的代碼註冊。標識設備。
  • @參數字符串$ MSGTYPE類型此消息的(一個應用程序的東西)。這也將用作collapse_key。
  • @參數字符串$ MessageText中的信息發送到手機上。
  • @返回布爾成功返回真或假失敗* /

功能sendMessageToPhone($ AUTHCODE, $ deviceRegistrationId,$ MSGTYPE, $ MessageText中){

$headers = array('Authorization: GoogleLogin auth=' . $authCode); 
$data = array(
    'registration_id' => $deviceRegistrationId, 
    'collapse_key' => $msgType, 
    'data.message' => $messageText   
); 

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

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

$response = curl_exec($ch); 

// for debugging the request 
//var_dump(curl_getinfo($ch)); 
// var_dump($response); 

curl_close($ch); 

return $response; }