我知道有很多帖子都有同样的问题,但是在读完所有帖子之后,我找不到问题的原因。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' –