使用GCM向多个设备发送通知

时间:2013-04-16 11:25:13

标签: php google-cloud-messaging

我需要通过服务器向多个设备发送通知,但它对我不起作用,它适用于一个。所以我试图在获得所有gcm_regid之后进行循环,但它没有work.Here是我的代码: 主页上的功能:

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(全部).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);

这就是全部!

0 个答案:

没有答案