Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Gcm消息未发送给超过1000个用户_Php_Android_Arrays_Google Cloud Messaging - Fatal编程技术网

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

Php Gcm消息未发送给超过1000个用户,php,android,arrays,google-cloud-messaging,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:

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

你有没有试过发送5次,每次1000次?也许您可以在每个批之间添加一些延迟,因此不要在发送第一批消息后立即发送,而是等待它处理。只是给你一些建议我已经尝试发送了1000次,因此我提出了上述问题。请参阅以下帖子: