GCM 消息未发送给超过 1000 个用户


Gcm message not sent to more that 1000 users

Gcm 消息不会发送给超过 1000 个用户,GCM 有自己的向 1000 个用户发送消息

的限制,所以这个想法是将用户分成 1000 个批次,我试过了,但是 GCM 消息仅由前 1000 个用户接收,我们如何分批发送消息,每个 1000 个, 总共说 5000 个用户,以便所有用户都收到消息我是 PHP 新手请解释工作

<?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', 'xxxxxxxxxxxxxxxxxxxxxxxxx');
    $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 = '';
function getGCMCount(){
 $total = "";
 $query = "SELECT count(gcm_regId) as total FROM gcm_users";
    while($query_row = mysql_fetch_assoc($query_run)) {
          $total = $query_row['total'] / 1000;     
    }  
    return $total;
}
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'];
//echo $i . "</br>" ; 
            }
        }      

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

        $pushStatus = array();
//echo "</br> counnt of messages send is". count($pushStatus);
        foreach($gcmRegIds as $key=>$val)
{
$message = array('price' =>  $pushMessage);
//$message1 = array($key=>$val);
//$c = (array_merge($message ,$message1 ));
$pushStatus[] = sendPushNotification($val, $message);
//echo $key;
}
    }   
}
?>
<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  . "<br>"?></h3></p>
    </form>
    </body>
</html>  

请参阅此代码段,了解如何向 1000 多个用户发送消息

<?php 
    //Sample for sending notification to more than 1000 users
    $mPushNotification = $push->getMessage();

    $stmt = $this->con->prepare("SELECT gcm_regid FROM gcm_users");
    $stmt->execute();
    $result = $stmt->get_result();
    $tokens = array();
    while ($token = $result->fetch_assoc()) {
         array_push($tokens, $token['gcm_regid']);
    }

    $firebase = new Firebase();
    $total = count($tokens);
    $groups = ceil($total/800);
        $currentval=ceil($total/$groups);
        $firebase = new Firebase(); 
        for ($i=0; $i <$groups; $i++) { 
            $val=($i*$currentval)+1;
            $total = ($i+1)*$currentval;
            $resToken = getSpecificToken($val,$total);
            $result1 = $firebase->send($resToken, $mPushNotification);
        }
    function getSpecificToken($upper,$lower)
    {
            $stmt = $this->con->prepare("SELECT * FROM gcm_users LIMIT $upper,$lower");
            $stmt->execute();
            $result = $stmt->get_result();
            $tokens = array();
            while ($token = $result->fetch_assoc()) {
                array_push($tokens, $token['gcm_regid']);
            }
        return $tokens;
    }
 function getMessage() {
        $res = array();
        $res['data']['id'] = 1;
        $res['data']['title'] = "TestTitle";
        $res['data']['message'] = "TestMessage : Hello";
        return $res;
    }
?>
//Firebase File
<?php 
class Firebase {
    public function send($registration_ids, $message) {
        $fields = array(
            'registration_ids' => $registration_ids,
            'data' => $message,
        );
        return $this->sendPushNotification($fields);
    }
    /*
    * This function will make the actual curl request to firebase server
    * and then the message is sent 
    */
    private function sendPushNotification($fields) {
        //importing the constant files
        require_once 'Config.php';
        //firebase server url to send the curl request
        $url = 'https://fcm.googleapis.com/fcm/send';
        //building headers for the request
        $headers = array(
            'Authorization: key=' . FIREBASE_API_KEY,
            'Content-Type: application/json'
        );
        //Initializing curl to open a connection
        $ch = curl_init();
        //Setting the curl url
        curl_setopt($ch, CURLOPT_URL, $url);
        //setting the method as post
        curl_setopt($ch, CURLOPT_POST, true);
        //adding headers 
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //disabling ssl support
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //adding the fields in json format 
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        //finally executing the curl request 
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        //Now close the connection
        curl_close($ch);
        //and return the result 
        return $result;
    }
}
?>