簡體   English   中英

如何使用XHR(Ajax)通過GCM發送推送通知

[英]How to Send a push notification through GCM using XHR (Ajax)

這是我的curl命令,用於通過終端發送通知。 它正常工作,但是如何在單擊按鈕時發送通知(使用XHR請求)

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

您可以編寫一個php腳本來實現此目的,並且只需向客戶端發出XHR請求即可,例如javascript或您選擇的任何其他內容。 這是我幾個月前做的PHP腳本,它正在工作。

    <?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);
    ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM