отправить gcm-сообщение в группах из 1000 пользователей всем 10000 пользователям

Я использую следующий код для отправки gcm-сообщения с помощью php и mysqldb. Я успешно отправил gcm-сообщение и получил его на устройстве, однако в руководстве Android указано, что gcm-сообщение должно быть отправлено в партии по 1000 каждый http://developer.android. ком / обучение / cloudsync / gcm.html # многоадресного

теперь мой вопрос заключается в том, как мы можем отправлять сообщения gcm в тысячах 1000 в базу данных, например, 10 000 зарегистрированных пользователей.

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

Related of "отправить gcm-сообщение в группах из 1000 пользователей всем 10000 пользователям"

Сделайте $ gcmRegIds как 2D-массив, а затем запустите его, чтобы нажать 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); } } 

Вот что я сделал для моего сценария для решения для FCM. Вы также можете игнорировать некоторые части кода

 <?php $saved_tokens=array(); // could be more than 1000 token the maximum size as per google FCM requirement //$count=1; $ID_SETS = array_chunk($saved_tokens, 1000); // make a group of 1000 items in each set foreach ($ID_SETS as $value_ids) { // echo "Chunk ".$count."<br>"; sendFCM_Notification("what_ever_parameter_data",$value_ids); foreach ($value_ids as $value_i ) { // you can remove this part its not neccessary //echo " ---- contents of Chunk ".$count ."with Value".$value_i."<br>"; } // $count++; } function sendFCM_Notification($what_ever_parameter,$reg_ids_array){ $url = 'https://fcm.googleapis.com/fcm/send'; $fields = array( 'registration_ids' => $reg_ids_array, 'data' => $what_ever_parameter ); $headers = array( 'Authorization:key = YOUR_KEY ', 'Content-Type: application/json' ); $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_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 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($ch)); } curl_close($ch); return $result; } ?>