这是使用 cURL 从终端发出请求的命令。有用!但是,我不知道如何通过 XHR (Ajax) 发出相同的请求。任何帮助将不胜感激。
curl --header "Authorization: key=ABC123" --header "Content-Type: application/json" https://android.googleapis.com/gcm/send -d "{\"registration\_ids\":[\"DFG456\"]}"
最佳答案
这是PHP代码(需要安装php5-curl
包):
// curl.php
$data = ["registration_ids" => ["DFG456"]];
$dataString = json_encode($data);
$curl = curl_init('https://android.googleapis.com/gcm/send');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataString);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: key=ABC123',
'Content-Length: ' . strlen($dataString)
]);
$result = curl_exec($curl);
print_r($result);
和 ajax 请求(使用 jquery):
// index.html
<button id="send">Send</button>
<script>
$("#send").click(function() {
$.get("curl.php", function(data) {
console.log(data);
});
});
</script>
关于android - 如何使用 XHR (Ajax) 发送推送通知请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34824924/