你好,我試圖在我的android應用上使用GCM。來自GCM服務器的401錯誤
我的問題是,當試圖發送消息時,我得到401錯誤,未經授權的錯誤。
我做在谷歌雲控制檯服務器應用的關鍵和遵循這一指示:http://developer.android.com/google/gcm/gs.html
這是我的代碼:
class GCM {
//put your code here
// constructor
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Content-Type: application/json',
'Authorization: key=**MY_KEY***'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
}
從我的應用我發送POST這個PHP代碼:
include_once './GCM.php';
$gcm = new GCM();
$message = array("status" => "1");
$ids = array('**my_gcm_id**');
$result = $gcm->send_notification($ids, $message);
echo $result;
我有一個類似的問題從我的服務器,並刪除我的IP也工作。奇怪的是,我能夠通過nodejs發送,但不能從我的.Net Web應用程序發送。我浪費了幾個小時,所以謝謝你的建議。 – raider33