2014-04-24 116 views
0

我試圖從我的服務器到谷歌雲服務器發送消息,但我有問題,它... 我得到服務器密鑰(主要針對服務器應用程序),將其設置爲驗證碼:谷歌Android雲通訊

$headers = array(
    'Authorization: key=' .My server key, 
    'Content-Type: application/json' 
); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 
$result = curl_exec($ch); 

後發短信,我有這樣的結果:

未經授權 錯誤401

我使用這種IP:78.47.150.20

,但當我使用測試IP 0.0.0.0/0我沒有任何問題....

回答

1
function sendNotification($registrationIdsArray, $messageData) { 
    $apiKey = "YOUR_API_KEY"; 

$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; 
} 
+0

abovfe鏈接給我{「multicast_id」:8142008928614681488,「成功」:1,「失敗」: 0,「canonical_ids」:0,「results」:[{「message_id」:「0:1411015000919118%5d450aecf9fd7ecd」}]}。再檢查一遍。 – Dev

+0

是的,我以後工作。謝謝 :) – user3201500