【发布时间】: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> ($message)</i></p><p>$msg</p></div>";
}else {
$inf= "<div id='message' class='updated'><p><b>".__('Message sent.','px_gcm')."</b><i> ($message)</i></p><p>".__('success:','px_gcm')." $suc ".__('fail:','px_gcm')." $fail </p></div>";
}
【问题讨论】: