0
我知道有很多帖子都有同樣的問題,但在讀完所有帖子之後,我找不到問題的原因。GoogleCloudMessaging PHP腳本總是返回NotRegistered
我寫了一個GCM客戶端來註冊我的設備,從我的服務器接收消息。它正在工作,我可以將註冊ID存儲在我的數據庫中。
我的問題是在服務器端。我用在什麼地方找到谷歌搜索的腳本,但我總是收到一個錯誤的結果:
{"multicast_id":5185959414153599391,"success":0,"failure":2,"canonical_ids":0,"results":[{"error":"NotRegistered"},{"error":"NotRegistered"}]}
的PHP代碼是(我使用的瀏覽器API,而不是服務器API的教程所講述的,但嘗試這兩個鍵的回報同樣的信息):
$this->db->SELECT('device_id');
$this->db->from('user');
$this->db->where('status = 1');
$this->db->where("id IN (".$param['first_friend'].",".$param['second_friend'].")");
$query_id = $this->db->get();
$result_id = $query_id->result_array();
$gcmRegIds = array(trim($result_id[0]['device_id']), trim($result_id[1]['device_id']));
if ($result[0]['username']) {
$name = $result[0]['username'];
} else {
$name = $result[0]['name'];
}
$pushMessage = $name." was created meeting to you.";
if(isset($gcmRegIds) && isset($pushMessage)) {
$message = array('message' => $pushMessage);
$pushStatus = $this->send_notification($gcmRegIds, $message);
}
我的功能,
function send_notification($registatoin_ids, $message) {
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
define("GOOGLE_API_KEY", "xxxxxxxxxxxxxxxxxxx");
$headers = array(
'Authorization: key=' . GOOGLE_API_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);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
我把真實的ID和關鍵,看看我做的權利。有任何想法嗎?
基本上談到要麼你的項目ID或設備ID有誤,請重新檢查它 –
@bornprogrammer,你的意思是項目id爲API KEY? –
這裏有兩件事 1)一個是項目ID,當你需要註冊設備ID時,它將從android端使用 2)當你發送推送消息到gcm服務器時需要api密鑰 –