This question is very common and I have seen some posts in the English Stack that exemplify (without being very objective) how to solve the problem. GCM limits the sending of the same message to 1000 devices registered in a database, all at once. If there are more than 1000 devices the message is not even sent. It would take a Loop that would separate the records into batches of 1000 records and make a leap every amount. If you have 5000 would be 5 reps of 1000 for the same message: Below is the PHP code that I got (and is the same on several sites) and that limits me to this amount, would anyone have any solution?
class GCM {
function __construct(){}
public function send_notification ($registatoin_ids, $data) {
require "includes/google_api_key.php";
// GOOGLE API KEY
define ("GOOGLE_API_KEY", $google_api_key);
$url = "https://android.googleapis.com/gcm/send";
$fields = array (
"registration_ids" => $registatoin_ids,
"data" => $data,
);
//var_dump($fields);
$headers = array(
"Authorization: key=".GOOGLE_API_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_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result_gcm=curl_exec($ch);
if($result_gcm===FALSE) {
die("Curl failed: ".curl_error($ch));
}
curl_close($ch);
//echo $result_gcm;
}
}
// Create connection
$conn = mysqli_connect($mysqlHost, $mysqlUser, $mysqlPwd, $mysqlDbname);
// Check connection
if (!$conn) {
die ("Connection failed: " . mysqli_connect_error());
}
$result = $conn->query("SELECT * FROM tbl_notification WHERE users_android_token IS NOT NULL AND users_android_token <> ''");
$android_tokens = array();
$x = 0;
if ($result -> num_rows > 0) { // Acredito que aqui seria
// o local para inserir um laço for
// output data of each row
while($row = $result -> fetch_assoc()) {
$android_tokens[] = $row["users_android_token"];
$x++;
}
} else {
echo "";
}
$conn -> close();
$title = $_POST['title'];
$msg = $_POST['message'];
$link = $_POST['link'];
if ($android_tokens != array()) {
$gcm = new GCM();
$data = array("title" => $title,"description" => $msg,"link" => $link);
$result_android = $gcm -> send_notification($android_tokens,$data);
}