问题描述
我正在使用以下代码使用PHP和MySQLDB发送GCM消息,我已经成功发送了GCM消息并在设备上接收到它,但是Android指南指出,GCM消息应以每个 http://develoder.android.com/training/cloudsync/cloudsync/gcm.html.html.html#multicast /p>
现在,我的问题是,我们如何将1000中大量的GCM消息发送到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>
推荐答案
使$ gcmregids成为2D数组,然后foreach将其推向sg:
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; } ?>
问题描述
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>
推荐答案
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); } }
其他推荐答案
Here is what i did for my scenario to resolve for FCM .You can also ignore some parts of the code
<?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; } ?>