【问题标题】:How to send a push notification request using XHR (Ajax)如何使用 XHR (Ajax) 发送推送通知请求
【发布时间】:2016-04-21 20:45:20
【问题描述】:

这是从终端发出请求的命令,带有 cURL。有用!但是,我不知道如何通过 XHR (Ajax) 发出相同的请求。任何帮助将不胜感激。

curl --header "Authorization: key=ABC123" --header "Content-Type: application/json" https://android.googleapis.com/gcm/send -d "{\"registration\_ids\":[\"DFG456\"]}"

【问题讨论】:

  • 您在服务器端使用哪种编程语言? PHP?
  • Javascript?我正在使用 Google Cloud Messaging (GCM),并且正在使用 parse.com 来托管应用程序。

标签: android jquery ajax curl


【解决方案1】:

这是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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 2016-04-18
    相关资源
    最近更新 更多