I am getting following error message when sending push notification to more than 1000 users:
"firebase Number of messages on bulk (1082) exceeds maximum allowed (1000)"
I have googled the issue and found that FCM can send only 1000 message per request. Solution: Split sender list in php, but unfortunately I am new to php and unable to achieve the looping.
My php script is:
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message,
'click_action' => ACTIVITY_CIRCULAR
);
$headers = array(
'Authorization:key = <MY_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;
}
$conn = mysqli_connect("localhost","username","password","databse_name");
$sql = " Select Token From users";
$result = mysqli_query($conn,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["Token"];
}
}
mysqli_close($conn);
$message = array("message" => "Hi This Arbaz Alam And You are using Android App Thanks.");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
Can you please help me in this regard and provide me a push_notification.php script that will split the users.
I have tried to split the message in chunks of 1000 but get no success:
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = <MY-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;
}
function send_message($tokens)
{
$message = array("message" => "AKTU Even Semester Result is updated");
$message_status = send_notification($tokens, $message);
echo $message_status;
}
$conn = mysqli_connect("localhost","username","password","database_name");
$sql = " Select Token From users";
$result = mysqli_query($conn,$sql);
$tokens = array();
$num_of_rows = mysqli_num_rows($result);
echo "$num_of_rows";
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["Token"];
if(count($tokens) == 1000){
send_message($tokens);
$tokens = array();
}
}
}
send_message($tokens);
mysqli_close($conn);
?>