2016-03-10 51 views
1

我試圖通過GCM發送循環消息以繞過谷歌的批量限制。PHP GCM 1000用戶循環

我在這裏發現了一些非常有用的答案,這是我用來發送批量消息給我的數據庫中的註冊用戶的最終php代碼。

然而,該方法1000之後停止,我得到的消息 對羣發郵件數(1016)超過允許的最大(1000)

有人能看到什麼錯用下面的代碼?

<?php 
session_start(); 
include_once 'connect.php'; 

function send_notification($con,$registatoin_ids, $message) { 
    $url = 'https://android.googleapis.com/gcm/send'; 

$msg = array 
(
'message' => "the message", 
'title'  => 'my title', 
'subtitle' => 'subtitle. subtitle', 
'msgcnt' => 3, 
'vibrate' => 1, 
'sound'  => 'default', 
'soundname' => 'beep.wav', 
'largeIcon' => 'large_icon', 
'smallIcon' => 'small_icon' 
); 
$fields = array 
(
'registration_ids' => $registatoin_ids, 
    'data'   => $msg 
); 

    $headers = array(
     'Authorization: key=' . apiKey, 
     'Content-Type: application/json' 
    ); 
    $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)); 
    $result = curl_exec($ch); 
    if ($result === FALSE) { 
     die('Curl failed: ' . curl_error($ch)); 
    } 
    curl_close($ch); 
    echo $result; 

} 

if(isset($_SESSION['login_user'])){ 
if(isset($_POST["submit"]) && isset($_POST["message"])) { 
$num=$con->query("SELECT gcm_regid from gcm_users")->rowCount(); 
$current_num=0; 
$message=$_POST["message"]; 
for($i=0;$i<$num/1000;$i++){ 

$query=$con->query("SELECT gcm_regid from gcm_users LIMIT $current_num,1000"); 

foreach($query as $data) { 
$row[]=$data["gcm_regid"]; 
} 

$pushStatus = send_notification($con,$row, $message); 
$current_num+=1000; 
} 
}else if(isset($_POST["logout"])){ 

if(session_destroy()) // Destroying All Sessions 
{ 
header("Location: login.php"); // Redirecting To Home Page 
} 
} 

?> 

謝謝

+0

我會檢查傳遞給send_notification的$ row數組的大小。你能確認它的尺寸是1000 –

+0

是的。我試圖改變1000到500,但最終我得到了同樣的錯誤信息:大量消息(1035)超過允許的最大數量(1000) – qwertyg

回答

2

可以使用GCM只發送每個請求的1000封電子郵件。將用戶分成更小的塊(讓每個塊有500個用戶:$current_num,500),並在2個或更多請求中發送電子郵件給他們。

+0

thnx爲您的答案,我知道我只能發送1000每個請求,在我的代碼我試圖做到這一點,但沒有結果。你能看到上面的代碼中有什麼錯誤嗎? thnx再次 – qwertyg