【问题标题】:PHP GCM 1000 users loopPHP GCM 1000 用户循环
【发布时间】:2016-03-10 08:50:01
【问题描述】:

我试图通过 GCM 循环发送一条消息,以绕过谷歌的批量限制。

我在这里找到了一些非常有用的答案,这是我用来向数据库中的注册用户发送批量消息的最终 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
}
}

?>

谢谢

【问题讨论】:

  • 我会检查您传递给 send_notification 的 $row 数组的大小。你能确认它的大小是 1000
  • 是的。我尝试将 1000 更改为 500,但最后我收到相同的错误消息:批量消息数 (1035) 超过允许的最大值 (1000)

标签: php android google-cloud-messaging


【解决方案1】:

您使用 GCM 每次请求只能发送 1000 封电子邮件。将您的用户拆分成更小的块(假设每个块 500 个用户:$current_num,500)并在 2 个或更多请求中向他们发送电子邮件。

【讨论】:

  • 感谢您的回答,我知道每个请求我只能发送 1000 个,在我的代码中我尝试这样做但没有结果。你能看出上面的代码有什么问题吗?再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 2016-02-28
  • 2015-06-30
  • 1970-01-01
  • 2016-04-21
  • 2016-08-26
相关资源
最近更新 更多