dongtuo5611 2015-01-24 18:01
浏览 28
已采纳

将1000个用户的gcm消息发送给所有10000个用户

i am using following code to send gcm message using php and mysqldb i have successfully send the gcm message and received it on the device however the android guidelines state that gcm message should be send in batch of 1000 each http://developer.android.com/training/cloudsync/gcm.html#multicast

now my question is how can we send gcm messages in lots of 1000 to a database of say 10,000 registered users.

<?php
require 'connect.php';

function sendPushNotification($registration_ids, $message) {

    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array(
        'registration_ids' => $registration_ids,
        'data' => $message,
    );

    define('GOOGLE_API_KEY', 'keys_value');

    $headers = array(
        'Authorization:key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    echo json_encode($fields);
    $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_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);
    if($result === false)
        die('Curl failed ' . curl_error());

    curl_close($ch);
    return $result;

}

$pushStatus = '';

if(!empty($_GET['push'])) {

    $query = "SELECT gcm_regId FROM gcm_users";
    if($query_run = mysql_query($query)) {

        $gcmRegIds = array();
        while($query_row = mysql_fetch_assoc($query_run)) {

            array_push($gcmRegIds, $query_row['gcm_regId']);

        }

    }
    $pushMessage = $_POST['message'];
    if(isset($gcmRegIds) && isset($pushMessage)) {

        $message = array('price' => $pushMessage);
        $pushStatus = sendPushNotification($gcmRegIds, $message);

    }   
}

?>

<html>
    <head>
        <title>Google Cloud Messaging (GCM) Server in PHP</title>
    </head>
    <body>
    <h1>Google Cloud Messaging (GCM) Server in PHP</h1>
    <form method = 'POST' action = 'send_all.php/?push=1'>
        <div>
            <textarea rows = 2 name = "message" cols = 23 placeholder = 'Messages to Transmit via GCM'></textarea>
        </div>
        <div>
            <input type = 'submit' value = 'Send Push Notification via GCM'>
        </div>
        <p><h3><?php echo $pushStatus ?></h3></p>
    </form>
    </body>
</html>
  • 写回答

2条回答 默认 最新

  • duan4739 2015-01-24 18:09
    关注

    Make $gcmRegIds to be a 2D array and then foreach it to push msg:

    if(!empty($_GET['push'])) {
    
        $query = "SELECT gcm_regId FROM gcm_users";
        if($query_run = mysql_query($query)) {
    
            $gcmRegIds = array();
            $i = 0;
            while($query_row = mysql_fetch_assoc($query_run)) {
                $i++;
                $gcmRegIds[floor($i/1000)][] = $query_row['gcm_regId'];
            }
    
        }
        $pushMessage = $_POST['message'];
        if(isset($gcmRegIds) && isset($pushMessage)) {
    
            $message = array('price' => $pushMessage);
            $pushStatus = array();
            foreach($gcmRegIds as $val) $pushStatus[] = sendPushNotification($val, $message);
    
        }   
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题