【问题标题】:how to fix GCM 1000 message limit,want to send message to a database of 10000? [duplicate]如何修复 GCM 1000 消息限制,想将消息发送到 10000 的数据库? [复制]
【发布时间】:2016-01-25 04:57:17
【问题描述】:

我正在使用以下代码使用 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>";
}

【问题讨论】:

    标签: php android mysql arrays


    【解决方案1】:

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

    【讨论】:

    • 你能告诉我如何在上面提到的代码中做到这一点
    • 您的代码中有一个循环,但您的条件 if($id == 010 && $id >= 1000) 是错误的。所以你的 if 包含循环的部分没有被执行,并且你的 else 部分总是被执行,这导致了这个错误
    • 先生,我不知道如何解决它,,,如果($id == 010 && $id >= 1000)请您纠正这个循环条件
    • 函数 px_getIds() { 全局 $wpdb; $px_table_name = $wpdb->prefix.'gcm_users'; $devices = 数组(); $sql = "从 $px_table_name 中选择 gcm_regid"; $res = $wpdb->get_results($sql); if ($res != false) { foreach($res as $row){ array_push($devices, $row->gcm_regid); } } 返回 $devices; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多