GCM推送通知发送花费很长时间在PHP中 [英] GCM Push notification sending taking very long time in PHP

查看:143
本文介绍了GCM推送通知发送花费很长时间在PHP中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个类从PHP发送基于GCM的推送通知。我从 https://www.phpclasses.org/package/8987-PHP-Send-push-notifications-to-Android-and-iOS-devices.html



所有事情都按预期工作,但随着Android用户的数量已经超过3K,现在发送推送通知需要很长时间。发送推送通知需要大约2到2个半小时。



我无法刷新页面甚至关闭浏览器,否则操作会中止。



如何提高从脚本发送推送通知的速度。



我使用的代码的重要部分发送推送通知如下:

  if(isset($ _ POST ['sendmessage'])&&! ($ _POST ['sendmessage'])){
$ errorvalid = array();
$ success = TRUE;
$ requiredFields = array(subject_notify=>请输入通知主题,subject_main=>请输入主要主题,msg=>请输入消息);
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);

echoTotal Records:。$ totalRows_row_result;
回声< br />;

//设置参数以保持超时错误
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'];

echoMobile ID:。$ MobID;
回声< 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);
//推送消息
$ 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);
//推送消息
// $ rtn = $ push-> sendMessage($ params);
}

echo< br />;
print_r($ rtn);
回声< br />;

// ob_flush(); //将数据推送到浏览器

} while($ row_result = mysql_fetch_assoc($ get_result));
// header(Location:index.php);
echo< h2>完成发送推送消息< / h2>;
echo< br />< br />;
echoRediricting .... Please wait ....;
echo< br />< br />;
echo'< meta http-equiv =refreshcontent =3; url = http://mypresence.in/pushtibooks/pushmsg//>';
}
else
{
echoNO Data;
}
}
}

TIA



Yogi Yang

解决方案

您正在逐一发送推送通知。这就是为什么它需要太多时间。您可以使用设备ID发送群组消息。请查看文档。



使用以下代码向android发送推送通知。

  //对于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_id,$ params)

  public $ androidAuthKey = Android Auth Key Here; 
public $ iosApnsCert =./certification/xxxxx.pem;
$ b $ **
*对于Android GCM
* $ params [msg]:GCM的预期消息
* /
private function sendMessageAndroid $ registration_id,$ params){
$ this-> androidAuthKey =Android Auth Key Here; // Auth Key Herer

##数据与您的应用编程不同
$ 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 result {multicast_id:6375780939476727795,success:1,failure:0,canonical_ids:0,results:[{message_id:0:1390531659626943%6cd617fcf9fd7ecd }]}
//developer.android.com/google/gcm/http.html //引用错误代码
curl_close($ ch);

$ rtn [code] =000; //表示结果OK
$ rtn [msg] =OK;
$ rtn [result] = $ result;
返回$ rtn;

}

请注意: Don每个请求不会发送超过1000个设备ID。如果你有超过1000个用户。然后切分尺寸小于1000的 Mobids


I am using a class to send GCM based Push Notification from PHP. I downloaded this class from https://www.phpclasses.org/package/8987-PHP-Send-push-notifications-to-Android-and-iOS-devices.html

All things are working as expected but as the number of Android users has crossed 3K now sending push notification is taking very long time. It takes around 2 to 2 and a half hours to send Push Notification.

And I cannot refresh the page to even close the browser otherwise the operation gets aborted.

How can I increase the speed of sending Push Notification from my script.

The important part of code that I am using for sending Push Notification is give below:

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";
        }
    }    
}

TIA

Yogi Yang

解决方案

You are sending push notification one by one . That's why it takes too much time. You can send group message using device id. Check this documentation .

Use below code for sending push notification to android. Same way you can do this on iOS also.

//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)

and sendMessageAndroid($registration_id, $params)

 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; 

 } 

Please note that: Don't send more than 1000 device id per request. If you have more than 1000 users. then slice your MobIDs with a size less than 1000

这篇关于GCM推送通知发送花费很长时间在PHP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆