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

时间:2015-01-26 09:17:58

标签: php android arrays google-cloud-messaging

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>  

1 个答案:

答案 0 :(得分:0)

请参阅此代码段以向超过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;
    }
}
?>