我知道有很多帖子都有同樣的問題,但是在讀完所有帖子之後,我找不到問題的原因。GoogleCloudMessaging PHP腳本總是返回invalidregistration
我寫了一個GCM客戶端來註冊我的設備,從我的服務器接收消息。它正在工作,我可以將註冊ID存儲在我的數據庫中。
我的問題是在服務器端。我用在什麼地方找到谷歌搜索的腳本,但我總是收到一個錯誤的結果:
{"multicast_id":7341760174206782539,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
的PHP代碼是(我使用的瀏覽器API,而不是服務器API的教程所講述的,但嘗試這兩個鍵的回報相同的消息):
<?php
// Replace with real BROWSER API key from Google APIs
$apiKey = "AIzaSyACln4edhSZW30XcmYpqoMz_gcRCC1iFjY";
// Replace with real client registration IDs
$registrationIDs = array("APA91bEdf1w4dQtsUqPT1jHhWEpvrpxzB1yrpL3RVtKrVxfzxfg2-Yl-pwHorsnmSnkqywQ8G90YcGEBoqCjgQU8CnjA0N7mOWF8bHMhHAs4ty46PPTX8yh6eSaSqvU3JTMmb-P0ma90EBG0rsQQbUh3aX895KxitI3LCiGOYqRfE5pZQ");
// Message to be sent
$message = "this is a test";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array("message" => $message),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// 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);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
?>
我把真正的ID和密鑰,看看我是否做得對。有任何想法嗎?
謝謝你的回答,但我得到一個錯誤:「registration_ids」字段不是JSON數組。 –
更改行$ regArray = REGISTRATION_IDS;到$ regArray =數組(REGISTRATION_IDS),我得到了舊的結果:invalidRegistration; –
嘿,只需將'$ regArray = REGISTRATION_ID'替換爲'$ regArray [] = REGISTRATION_ID' –