Javascript 如何使用XHR(Ajax)通过GCM发送推送通知
这是我的curl命令,用于通过终端发送通知。它工作正常,但如何在点击按钮时发送通知(使用XHR请求)Javascript 如何使用XHR(Ajax)通过GCM发送推送通知,javascript,ajax,push-notification,xmlhttprequest,google-cloud-messaging,Javascript,Ajax,Push Notification,Xmlhttprequest,Google Cloud Messaging,这是我的curl命令,用于通过终端发送通知。它工作正常,但如何在点击按钮时发送通知(使用XHR请求) 您可以编写一个php脚本来实现这一目的,只需使用客户端发出一个XHR请求,就像javascript或您选择的任何其他脚本一样。 这是我的php脚本,我几个月前做的,它正在工作 <?php $message = "\": 3,\"title\": \"High score alert!\",\"\": true,\"custom_field\": { \"score\": 5
您可以编写一个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);
?>
thanx,谢谢你的回答,但我不想使用php。有什么方法可以使用javascriptis javascript或你来实现这一点吗?是的,我也可以使用ajax。。。。。有没有办法用ajax做到这一点,jquery@Asim KhanPlease read
<?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);
?>