用PHP发送GCM推送通知需要很长时间
我使用一个类从PHP发送基于GCM的推送通知。我从网上下载了这个课程 所有事情都如预期的那样进行,但由于Android用户数量已经超过3K,现在发送推送通知需要很长时间。发送推送通知大约需要2到2个半小时 我甚至无法刷新页面以关闭浏览器,否则操作将中止 如何提高从脚本发送推送通知的速度 我用于发送推送通知的代码的重要部分如下所示:用PHP发送GCM推送通知需要很长时间,php,android,push-notification,notifications,google-cloud-messaging,Php,Android,Push Notification,Notifications,Google Cloud Messaging,我使用一个类从PHP发送基于GCM的推送通知。我从网上下载了这个课程 所有事情都如预期的那样进行,但由于Android用户数量已经超过3K,现在发送推送通知需要很长时间。发送推送通知大约需要2到2个半小时 我甚至无法刷新页面以关闭浏览器,否则操作将中止 如何提高从脚本发送推送通知的速度 我用于发送推送通知的代码的重要部分如下所示: if (isset($_POST['sendmessage']) && !empty($_POST['sendmessage'])) { $e
if (isset($_POST['sendmessage']) && !empty($_POST['sendmessage'])) {
$errorvalid = array();
$success = TRUE;
$requiredFields = array("subject_notify" => "Please Enter Notify Subject.", "subject_main" => "Please Enter Main Subject.", "msg" => "Please Enter Message");
foreach ($requiredFields as $fld => $msg) {
$v = $_POST[$fld];
if (empty($v)) {
$success = false;
$errorvalid[$fld] = $msg;
} else {
$errorvalid[$fld] = '';
$$fld = $v;
}
}
if($success)
{
$get_result = mysql_query("SELECT ps_mobile_id, ps_service_type FROM push_service", $con);
$row_result = mysql_fetch_assoc($get_result);
$totalRows_row_result = mysql_num_rows($get_result);
echo "Total Records: ".$totalRows_row_result;
echo "<br/>";
//Set parameters to hold time out error
set_time_limit(0);
error_reporting(E_ALL);
//ob_implicit_flush(TRUE);
//ob_end_flush();
if($totalRows_row_result > 0) {
$push = new pushmessage();
do {
$MobID = $row_result['ps_mobile_id'];
$MobType = $row_result['ps_service_type'];
echo "Mobile ID: ".$MobID;
echo "<br/>";
if($MobType == 1)
{
//Android
$params = array("pushtype"=>"android", "msg"=>$msg, "registration_id"=>$MobID, "subject_main"=>$subject_main, "subject_notify"=>$subject_notify, );
$rtn = $push->sendMessage($params);
//Push the message
$rtn = $push->sendMessage($params);
}
else
{
//iOS
//$params = array("pushtype"=>"android", "msg"=>$msg, "registration_id"=>$MobID, //"subject_main"=>$subject_main, "subject_notify"=>$subject_notify, );
//$rtn = $push->sendMessage($params);
//Push the message
//$rtn = $push->sendMessage($params);
}
echo "<br/>";
print_r($rtn);
echo "<br/>";
//ob_flush(); //Push data to Browser
}while ($row_result = mysql_fetch_assoc($get_result));
//header("Location: index.php");
echo "<h2>Completed Sending Pusht Message</h2>";
echo "<br/><br/>";
echo "Rediricting.... Please wait....";
echo "<br/><br/>";
echo '<meta http-equiv="refresh" content="3;url=http://mypresence.in/pushtibooks/pushmsg/" />';
}
else
{
echo "NO Data";
}
}
}
if(设置($\u POST['sendmessage'])和&!空($\u POST['sendmessage'])){
$errorvalid=array();
$success=TRUE;
$requiredFields=数组(“主题通知”=>“请输入通知主题”,“主题通知”=>“请输入主要主题”,“消息”=>“请输入消息”);
foreach($requiredFields作为$fld=>$msg){
$v=$_POST[$fld];
如果(空($v)){
$success=false;
$errorvalid[$fld]=$msg;
}否则{
$errorvalid[$fld]='';
$$fld=$v;
}
}
如果($成功)
{
$get\U result=mysql\U查询(“从推送服务中选择ps\U移动\U id、ps\U服务\U类型”,$con);
$row\u result=mysql\u fetch\u assoc($get\u result);
$totalRows\u row\u result=mysql\u num\u rows($get\u result);
回显“总记录:”.$totalRows\u row\u结果;
回声“
”;
//设置参数以保持超时错误
设置时间限制(0);
错误报告(E_全部);
//ob_隐式_刷新(真);
//ob_end_flush();
如果($totalRows\u row\u result>0){
$push=新的pushmessage();
做{
$MobID=$row_result['ps_mobile_id'];
$MobType=$row_result['ps_service_type'];
echo“Mobile ID:”.$MobID;
回声“
”;
如果($MobType==1)
{
//安卓
$params=array(“pushtype”=>“android”、“msg”=>$msg、“registration\u id”=>$MobID、“subject\u main”=>$subject\u main、“subject\u notify”=>$subject\u notify,);
$rtn=$push->sendMessage($params);
//推送消息
$rtn=$push->sendMessage($params);
}
其他的
{
//iOS
//$params=array(“pushtype”=>“android”、“msg”=>$msg、“注册id”=>$MobID、//“subject\u main”=>$subject\u main、“subject\u notify”=>$subject\u notify,);
//$rtn=$push->sendMessage($params);
//推送消息
//$rtn=$push->sendMessage($params);
}
回声“
”;
印刷费($rtn);
回声“
”;
//ob_flush();//将数据推送到浏览器
}而($row_result=mysql_fetch_assoc($get_result));
//标题(“Location:index.php”);
echo“已完成发送Pusht消息”;
回声“
”;
echo“正在重新定价…请稍候…”;
回声“
”;
回声';
}
其他的
{
回声“无数据”;
}
}
}
短暂性脑缺血发作
Yogi Yang您正在逐个发送推送通知。这就是为什么要花太多时间。您可以使用设备id发送组消息。请查看文档 使用以下代码向android发送推送通知。同样,你也可以在iOS上这样做
//For andriod
$get_result = mysql_query("SELECT ps_mobile_id FROM push_service where ps_service_type = 1", $con);
// $row_result = mysql_fetch_assoc($get_result);
$totalRows_row_result = mysql_num_rows($get_result);
$MobIDs=array();
while($row = mysql_fetch_assoc($get_result)){
$MobIDs[] = $row;
}
$params = array("pushtype"=>"android", "msg"=>$msg, "subject_main"=>$subject_main, "subject_notify"=>$subject_notify, );
$rtn = $push->sendMessageAndroid($MobIDs, $params)
和sendMessageAndroid($registration\u id,$params)
请注意:每个请求发送的设备id不要超过1000个。如果您有超过1000个用户。然后将你的
MobIDs
切成小于1000的薄片你是在一个接一个地发送推送通知吗?谢谢Vinayak。它就像一个符咒。我必须在您建议的代码中将“'registration\u id'=>array($registration\u id)”更改为“'registration\u id'=>$registration\u id”。
public $androidAuthKey = "Android Auth Key Here";
public $iosApnsCert = "./certification/xxxxx.pem";
/**
* For Android GCM
* $params["msg"] : Expected Message For GCM
*/
private function sendMessageAndroid($registration_id, $params) {
$this->androidAuthKey = "Android Auth Key Here";//Auth Key Herer
## data is different from what your app is programmed
$data = array(
'registration_ids' => array($registration_id),
'data' => array(
'gcm_msg' => $params["msg"]
)
);
$headers = array(
"Content-Type:application/json",
"Authorization:key=".$this->androidAuthKey
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
//result sample {"multicast_id":6375780939476727795,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1390531659626943%6cd617fcf9fd7ecd"}]}
//http://developer.android.com/google/gcm/http.html // refer error code
curl_close($ch);
$rtn["code"] = "000";//means result OK
$rtn["msg"] = "OK";
$rtn["result"] = $result;
return $rtn;
}