2013-03-08 24 views
3

我想轉換PHP腳本(送在Android設備推送通知) 中一款Android HTTP POST代碼。轉換一個「PHP腳本來發送推送通知」,在Android的HTTP POST

PHP腳本(工作罰款)

印刷響應:

{ 「multicast_id」:7657471480142898452, 「成功」:1, 「失敗」:0, 「canonical_ids」:0 「結果」:[{ 「MESSAGE_ID」: 「0:1362731818787207%38894628f9fd7ecd」}]}

<?php 
$registrationIds = array("APA91bH1-35qLot...."); //device registration id 

$apiKey = "AIzaSyCrKrzFl..."; //"YOUR_BROWSER_API_KEY"; 

$response = sendNotification($apiKey,$registrationIds,array('message' => $message, 'tickerText' => $tickerText, 'contentTitle' => $contentTitle, "contentText" => $contentText)); 

echo $response; 

function sendNotification($apiKey, $registrationIdsArray, $messageData) { 
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey); 
$data = array(
'data' => $messageData, 
'registration_ids' => $registrationIdsArray 
); 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 

$response = curl_exec($ch); 
curl_close($ch); 

return $response; 

} 

?> 

的Android代碼(不工作):

打印響應:

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

public boolean sendMessage() { 

     String serviceUrl = "https://android.googleapis.com/gcm/send"; 

     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("registration_ids", "APA91bH1-35qLotyb......")); 
     nameValuePairs.add(new BasicNameValuePair("data", "this is a test message")); 

     try { 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(serviceUrl); 
      httpPost.setHeader("Authorization", "AIzaSyCrKrzFlPbauq......"); 
      httpPost.setHeader("Content-Type", "application/json"); 
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      json_str = EntityUtils.toString(httpEntity); 

     } catch (UnsupportedEncodingException e) { 
      json_str = "Can't connect to server"; 
     } catch (MalformedURLException e) { 
      json_str = "Can't connect to server"; 
     } catch (IOException e) { 
      json_str = "Can't connect to server"; 
     } 

     Log.i("***sendMessage", json_str); 
     return true; 
    } 
+0

你好,我可以得到教程或一些代碼從你的android推送通知?因爲我已經閱讀了所有的教程,谷歌的一些菜單與教程不一樣。謝謝 – 2013-12-09 03:23:42

回答

2

你忘了你的API密鑰之前key=前綴。 應該是

httpPost.setHeader("Authorization", "key=AIzaSyCrKrzFlPbauq......"); 

這就是爲什麼你得到了401錯誤。 儘管你有更多的錯誤。即使您的內容類型被聲明爲JSON,您也不會發送JSON字符串。

+0

謝謝伊蘭,我已經做了兩個更改,因爲根據你,現在我的代碼工作正常。 – Deven 2013-03-09 12:38:19

+0

不客氣! – Eran 2013-03-09 13:22:58