我需要通过服务器向多个设备发送通知,但它对我不起作用,它对一个设备正常工作。所以我在得到所有设备后尝试创建一个循环,gcm_regid
但它不起作用。这是我的代码:函数在主页上:
function sendPushNotificationToAll(){
var data = $('#form').serialize();
$.ajax({
url: "send_message(all).php",
type: 'GET',
data: data,
beforeSend: function() {
},
success: function(data, textStatus, xhr) {
$('.txt_message').val("");
},
error: function(xhr, textStatus, errorThrown) {
}
});
return false;
}
和 send_message(all).php :
include_once './config.php';
if (isset($_GET["message"])) {
$message = $_GET["message"];
mysql_connect("localhost","root","");
mysql_select_db("gcm");
$fetch=mysql_query("SELECT * FROM gcm_users");
$ids_array=array();
while($row = mysql_fetch_array($fetch))
{
$ids_array[]=$row['gcm_regid'];
}
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $ids_array,
data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
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 ) );
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
ie('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
当我点击发送时,没有任何反应。
更新 好的,所以回答我自己的问题:此代码在线缺失,即:
$message = array("price" => $message);
就这样!