2013-02-06 144 views
0

我是新來的android和我試圖在我的應用程序開發推送通知。不能發送C2DM消息到設備

我已經閱讀了這麼多文章,並且能夠生成設備註冊密鑰和認證密鑰。

但是當我試圖發送推送通知消息到設備我有下面的消息。

<HTML> 
<HEAD> 
<TITLE>Unauthorized</TITLE> 
</HEAD> 
<BODY BGCOLOR="#FFFFFF" TEXT="#000000"> 
<H1>Unauthorized</H1> 
<H2>Error 401</H2> 
</BODY> 
</HTML> 

和下面是我使用的發送消息,其PHP代碼:

function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText) { 

$headers = array('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); 

echo "<pre>"; 
print_r($response); die; 

} 

sendMessageToPhone($auth_token, $device_id , "UTF-8","hello"); 

我也能夠通過emaid「谷歌雲端通訊Android」的服務,我用來生成認證令牌。

回答