0

我使用一個類從PHP發送基於GCM推送通知。我下載了這個類從https://www.phpclasses.org/package/8987-PHP-Send-push-notifications-to-Android-and-iOS-devices.htmlGCM推送通知發送服用很長一段時間在PHP

,但所有的事情都按預期工作作爲Android用戶的數量已超過3K現在發送推送通知正在很長的時間。發送推送通知大約需要2到2個半小時。

我不能刷新頁面,即使關閉瀏覽器,否則操作被中止。

我怎麼能增加從我的腳本發送推送通知的速度。

代碼的重要組成部分,我使用發送推送通知低於給:

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

瑜珈楊

+0

你通過一個發送pushnotification一個切片的MobIDs? –

回答

1

你被一個發送推送通知之一。這就是爲什麼它需要太多時間。您可以使用設備ID發送羣組消息。檢查this文檔。

下面的代碼用於發送推送通知到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_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; 

} 

請注意:不要每次請求發送超過1000設備ID。如果你有超過1000個用戶。然後用尺寸小於1000

+0

謝謝Vinayak。它像一個魅力。我必須在您建議的代碼中將「'registration_ids'=> array($ registration_id)」更改爲「'registration_ids'=> $ registration_id」。 –