0

This is my curl command to send notification through terminal. It works properly but how to send notification on button click(using XHR request)

curl --header "Authorization: key=AIzaSyCjrU5SqotSg2ybDLK0dMusvY" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"f5xzshqcfDE2qiKGJu858nFhqGCuk0uuUC6vm\"]}"
R C
  • 461
  • 4
  • 17

1 Answers1

0

You can write a php script to serve the purpose and just make an XHR request with client ,like javascript or any other of your choice. Here is my php script I did it some months ago and it is working.

    <?php
    $message = "\": 3,\"title\": \"High score alert!\",\"\": true,\"custom_field\": { \"score\": 51,\"headlines\": \"And now for something completely different...\" }}";
    $apiKey = "Your Key";
    $registrationIDs = array("Registration id");
    $url = 'https://android.googleapis.com/gcm/send';
    // Set POST variables
    $notify = array(
       "alert"=> "great match!",
      "title"=> "Portugal vs. Denmark",
      "icon" => "myicon",
      "badge"=>3,
      "vibrate"=>true,
    "notification"=>"message"
    );

    $fields = array(
    'registration_ids' => $registrationIDs,
    'data' => array( "payload"  => $notify,
    "notification"=>json_encode($notify)));
    $headers = array(
    'Authorization: key=' . $apiKey,
    'Content-Type: application/json'
    );
    $ch = curl_init(); // Open connection
    curl_setopt($ch, CURLOPT_URL, $url );
    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
    var_dump( $url );
    var_dump($headers);
    var_dump(json_encode( $fields ));
    $result = curl_exec($ch);   // Execute post
    if($result === false)
    die('Curl failed ' . curl_error());
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    $response = json_decode($result);
    print_r($response);
    ?>
Asim Khan
  • 572
  • 6
  • 34