发送GCM消息1000的用户组所有10000用户 [英] send gcm message in groups of 1000 users to all 10000 users
问题描述
我用下面的code。使用PHP和MySQLdb的发送GCM消息我已经成功地发送了GCM消息,并收到了该设备但是Android的准则规定应派遣一批1000每个<一个是GCM消息HREF =http://developer.android.com/training/cloudsync/gcm.html#multicast相对=nofollow> http://developer.android.com/training/cloudsync/gcm.html#multicast 一>
现在我的问题是我们如何才能发送GCM消息很多1000说万注册用户的数据库。
&LT; PHP
要求将connect.php;
功能sendPushNotification($ registration_ids,$消息){
$ URL ='https://android.googleapis.com/gcm/send';
$领域=阵列(
registration_ids'=&GT; $ registration_ids,
'数据'=&GT; $消息,
);
定义('GOOGLE_API_KEY','keys_value');
$标题=阵列(
授权:键='。 GOOGLE_API_KEY,
内容类型:应用程序/ JSON
);
回声json_en code($领域);
$ CH = curl_init();
curl_setopt($ CH,CURLOPT_URL,$网址);
curl_setopt($ CH,CURLOPT_POST,真正的);
curl_setopt($ CH,CURLOPT_HTTPHEADER,$头);
curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,假);
curl_setopt($ CH,CURLOPT_POSTFIELDS,json_en code($域));
$结果= curl_exec($ CH);
如果($结果===假)
死亡(卷曲失败curl_error());
curl_close($ CH);
返回$结果;
}
$ pushStatus ='';
如果(!空($ _ GET ['推'])){
$查询=选择gcm_regId从gcm_users;
如果($ query_run =请求mysql_query($查询)){
$ gcmRegIds =阵列();
而($ query_row = mysql_fetch_assoc($ query_run)){
array_push($ gcmRegIds,$ query_row ['gcm_regId']);
}
}
$ pushMessage = $ _ POST ['消息'];
如果(使用isset($ gcmRegIds)及&安培;使用isset($ pushMessage)){
$消息=阵列('价格'=&GT; $ pushMessage);
$ pushStatus = sendPushNotification($ gcmRegIds,$消息);
}
}
?&GT;
&LT; HTML&GT;
&LT; HEAD&GT;
&LT;冠军&GT;谷歌云消息(GCM)在PHP&LT服务器; /标题&GT;
&LT; /头&GT;
&LT;身体GT;
&LT; H1&GT;谷歌云消息(GCM)服务器在PHP&LT; / H1&GT;
&LT;形式方法=POST行动=?send_all.php /推= 1'&GT;
&LT; DIV&GT;
&LT; textarea的行数= 2名称=消息COLS = 23占位符='消息通过GCM到发送'&GT;&LT; / textarea的&GT;
&LT; / DIV&GT;
&LT; DIV&GT;
&LT;输入类型=提交值='通过GCM发送推送通知'&GT;
&LT; / DIV&GT;
&LT; P&GT;&LT; H3&GT;&LT; PHP的echo $ pushStatus&GT;&LT; / H3&GT;&LT; / P&GT;
&LT; /形式GT;
&LT; /身体GT;
&LT; / HTML&GT;
请$ gcmRegIds是一个二维数组,然后它的foreach推味精:
如果(!空($ _ GET ['推'])){
$查询=选择gcm_regId从gcm_users;
如果($ query_run =请求mysql_query($查询)){
$ gcmRegIds =阵列();
$ i = 0;
而($ query_row = mysql_fetch_assoc($ query_run)){
$ I ++;
$ gcmRegIds [地板($ I / 1000)] [] = $ query_row ['gcm_regId'];
}
}
$ pushMessage = $ _ POST ['消息'];
如果(使用isset($ gcmRegIds)及&安培;使用isset($ pushMessage)){
$消息=阵列('价格'=&GT; $ pushMessage);
$ pushStatus =阵列();
的foreach($ gcmRegIds为$ VAL)$ pushStatus [] = sendPushNotification($ VAL,$消息);
}
}
i am using following code to send gcm message using php and mysqldb i have successfully send the gcm message and received it on the device however the android guidelines state that gcm message should be send in batch of 1000 each http://developer.android.com/training/cloudsync/gcm.html#multicast
now my question is how can we send gcm messages in lots of 1000 to a database of say 10,000 registered users.
<?php
require 'connect.php';
function sendPushNotification($registration_ids, $message) {
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registration_ids,
'data' => $message,
);
define('GOOGLE_API_KEY', 'keys_value');
$headers = array(
'Authorization:key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
echo json_encode($fields);
$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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result === false)
die('Curl failed ' . curl_error());
curl_close($ch);
return $result;
}
$pushStatus = '';
if(!empty($_GET['push'])) {
$query = "SELECT gcm_regId FROM gcm_users";
if($query_run = mysql_query($query)) {
$gcmRegIds = array();
while($query_row = mysql_fetch_assoc($query_run)) {
array_push($gcmRegIds, $query_row['gcm_regId']);
}
}
$pushMessage = $_POST['message'];
if(isset($gcmRegIds) && isset($pushMessage)) {
$message = array('price' => $pushMessage);
$pushStatus = sendPushNotification($gcmRegIds, $message);
}
}
?>
<html>
<head>
<title>Google Cloud Messaging (GCM) Server in PHP</title>
</head>
<body>
<h1>Google Cloud Messaging (GCM) Server in PHP</h1>
<form method = 'POST' action = 'send_all.php/?push=1'>
<div>
<textarea rows = 2 name = "message" cols = 23 placeholder = 'Messages to Transmit via GCM'></textarea>
</div>
<div>
<input type = 'submit' value = 'Send Push Notification via GCM'>
</div>
<p><h3><?php echo $pushStatus ?></h3></p>
</form>
</body>
</html>
Make $gcmRegIds to be a 2D array and then foreach it to push msg:
if(!empty($_GET['push'])) {
$query = "SELECT gcm_regId FROM gcm_users";
if($query_run = mysql_query($query)) {
$gcmRegIds = array();
$i = 0;
while($query_row = mysql_fetch_assoc($query_run)) {
$i++;
$gcmRegIds[floor($i/1000)][] = $query_row['gcm_regId'];
}
}
$pushMessage = $_POST['message'];
if(isset($gcmRegIds) && isset($pushMessage)) {
$message = array('price' => $pushMessage);
$pushStatus = array();
foreach($gcmRegIds as $val) $pushStatus[] = sendPushNotification($val, $message);
}
}
这篇关于发送GCM消息1000的用户组所有10000用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!