我知道有很多帖子都有同样的问题,但是在阅读完所有帖子后,我找不到问题的原因。
我编写了一个 GCM 客户端来注册我的设备以从我的服务器接收消息。它正在工作,我可以将注册 ID 存储在我的数据库中。
我的问题在服务器端。我正在使用在某处谷歌搜索的脚本,但我总是收到错误结果:
{"multicast_id":7341760174206782539,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
PHP 代码是(我使用的是 BROWSER API 而不是 SERVER 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;
?>
我放了真实的身份证和钥匙,看看我做得对不对。有任何想法吗?