如何修复GCM 1000消息限制,想要将消息发送到10000的数据库?

时间:2016-01-25 04:57:17

标签: php android mysql arrays

我使用以下代码使用PHP和MySQL发送GCM消息。请帮助我,以便它可以将1000多个GCM消息发送到10,000个注册用户的数据库。

在跨越1000个用户之前,这个脚本运行良好;但是在1000个用户之后,没有人接受推送 此脚本收到的错误是" 批量邮件数量(1082)超过允许的最大数量(1000)"

//GCM Send Notification
function px_sendGCM($message, $type, $regid) {
global $wpdb;
$px_table_name = $wpdb->prefix.'gcm_users';
$options = get_option('gcm_setting');
$apiKey = $options['api-key'];
$url = 'https://android.googleapis.com/gcm/send';
$result;
$id;

if($regid == 010) {
$id = px_getIds();
}else {
$id = $regid;
}

if($id == 010 && $id >= 1000){
$newId = array_chunk($id, 1000);
foreach ($newId as $inner_id) {
    $fields = array(
        'registration_ids' => $inner_id,
        'data' => array($type => $message) 
    );

    $headers = array(
        'Authorization' => 'key=' . $apiKey,
        'Content-Type' => 'application/json'
    );

    $result = wp_remote_post($url, array(
        'method' => 'POST',
        'headers' => $headers,
        'httpversion' => '1.0',
        'sslverify' => false,
        'body' => json_encode($fields) )
    );
}
}else {
$fields = array(
    'registration_ids' => $id,
    'data' => array($type => $message)
);

$headers = array(
    'Authorization' => 'key=' . $apiKey,
    'Content-Type' => 'application/json'
);

$result = wp_remote_post($url, array(
    'method' => 'POST',
    'headers' => $headers,
    'httpversion' => '1.0',
    'sslverify' => false,
    'body' => json_encode($fields))
    );

   }

$msg = $result['body'];
$answer = json_decode($msg);
$cano = px_canonical($answer);
$suc = $answer->{'success'};
$fail = $answer->{'failure'};
$options = get_option('gcm_setting');
if($options['debug'] != false){
$inf= "<div id='message' class='updated'><p><b>".__('Message    sent.','px_gcm')."</b><i>&nbsp;&nbsp;($message)</i></p><p>$msg</p></div>";
}else {
$inf= "<div id='message' class='updated'><p><b>".__('Message    sent.','px_gcm')."</b><i>&nbsp;&nbsp;($message)</i></p><p>".__('success:','px_gcm')." $suc  &nbsp;&nbsp;".__('fail:','px_gcm')." $fail </p></div>";
}

1 个答案:

答案 0 :(得分:0)

GCM允许您在单个请求中发送1000条消息。因此,如果您想向超过1000个客户发送消息,请使用循环,它将为每1000个客户调用您的函数px_sendGCM($ message,$ type,$ regid)

你的代码应该是:

//GCM Send Notification
function px_sendGCM($message, $type, $regid) {
global $wpdb;
$px_table_name = $wpdb->prefix.'gcm_users';
$options = get_option('gcm_setting');
$apiKey = $options['api-key'];
$url = 'https://android.googleapis.com/gcm/send';
$result;
$id;

if(sizeof($regid) == 010) {
$id = px_getIds(); //Can you post this function, what is this condition for?
}else {
$id = $regid;
}

if(sizeof($id) > 1000){
$newId = array_chunk($id, 1000);
foreach ($newId as $inner_id) {
    $fields = array(
        'registration_ids' => $inner_id,
        'data' => array($type => $message) 
    );

    $headers = array(
        'Authorization' => 'key=' . $apiKey,
        'Content-Type' => 'application/json'
    );

    $result = wp_remote_post($url, array(
        'method' => 'POST',
        'headers' => $headers,
        'httpversion' => '1.0',
        'sslverify' => false,
        'body' => json_encode($fields) )
    );
}
}else {
$fields = array(
    'registration_ids' => $id,
    'data' => array($type => $message)
);

$headers = array(
    'Authorization' => 'key=' . $apiKey,
    'Content-Type' => 'application/json'
);

$result = wp_remote_post($url, array(
    'method' => 'POST',
    'headers' => $headers,
    'httpversion' => '1.0',
    'sslverify' => false,
    'body' => json_encode($fields))
    );

   }

$msg = $result['body'];
$answer = json_decode($msg);
$cano = px_canonical($answer);
$suc = $answer->{'success'};
$fail = $answer->{'failure'};
$options = get_option('gcm_setting');
if($options['debug'] != false){
$inf= "<div id='message' class='updated'><p><b>".__('Message    sent.','px_gcm')."</b><i>&nbsp;&nbsp;($message)</i></p><p>$msg</p></div>";
}else {
$inf= "<div id='message' class='updated'><p><b>".__('Message    sent.','px_gcm')."</b><i>&nbsp;&nbsp;($message)</i></p><p>".__('success:','px_gcm')." $suc  &nbsp;&nbsp;".__('fail:','px_gcm')." $fail </p></div>";
}