How to send PUSH notification from the website using Google FCM and PHP in chunks

How to send PUSH notification from the website using Google FCM and PHP in chunks

We have already discussed How to send PUSH notification from the website using Google FCM and PHP you can check that post.

But most common problems occur when we have to send push notification to the thousand of devices. FCM only allows the user to send 1k notification at a time. Then what if we have thousands of users. what if we have to send all of them an instant notification. if you try to send the notification to all the users at once then it won’t work.

In that case, we have to send the data in the chunk to the FCM server.

Let’s suppose we have 10k users and we have to send them a notification, then we will divide 10k users into 10 small array containing 1k users.

In this tutorial, we will explain how you can send PUSH notification from the website using Google FCM and PHP in chunks.

  //post data
$notification_text = $_POST['notification_text'];
$title = $_POST['title'];

//you can insert post data into db for record here

$get_device_id = $object->get_device_id(); // get device id from database, i am fetching data using PDO you can use Msqli also(we did not mention the class file here) 

$loop =  count(array_chunk($get_device_id,1000));  // count array chunk 1000
$arrayChunk = array_chunk($get_device_id,1000);   // devide array in 1000 chunk

$device_id = array();
for($i=0; $i<$loop;$i++)
{
    foreach($arrayChunk[$i] as $all_device_id)
    {       
           
                $device_id[] =  $all_device_id->firebase_id;
    }

    $body=$notification_text;
    $customData=$url;

    $json_data = 
        [
            "registration_ids" => $device_id,
            "notification" => [
                "body" => $body,
                "title" => $title,
            ],
            "data" => [
                "extra" => $customData
            ]
        ];

 $data = json_encode($json_data); 

$url = 'https://fcm.googleapis.com/fcm/send';
//api_key in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
$server_key = 'AAAAJqUz63o:APA91bH9dBz6RihgEdvKhi5dXJyqEl234234dfdf25--0ylOoQswedsseWgpbR-AIkn7TVP7nP5kpPFxXJTRaAhv-d_It2vC9ejNhHeXOylo8oUm10yHm5h64yzm_PlsPuR';
//header with content_type api key
$headers = array(
    'Content-Type:application/json',
    'Authorization:key='.$server_key
);
// CURL request to route notification to FCM connection server (provided by Google)
    $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, $data);
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Oops! FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);
	unset($device_id); // unset the array value 
}

In the above code, you will be able to send data in chunks into the FCM server and can send thousand of notification at once.

Sending data in the chunk is useful in many scenarios. Here just because FCM has some limitations, we are sending the data in chunk.

For example, if we are trying to upload and insert all data at once then definitely that process is going to consume lots of memory of server which may sometime down the website, So it’s always recommend doing the bulk data activity in chunks.

Spread the love