android - How to send a push notification request using XHR (Ajax) -
this command make request terminal, curl. works! however, have no idea how make same request via xhr (ajax). appreciated.
curl --header "authorization: key=abc123" --header "content-type: application/json" https://android.googleapis.com/gcm/send -d "{\"registration\_ids\":[\"dfg456\"]}"
this php code (you need have installed php5-curl
package):
// 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);
and ajax request (using jquery):
// index.html <button id="send">send</button> <script> $("#send").click(function() { $.get("curl.php", function(data) { console.log(data); }); }); </script>
Comments
Post a Comment