0

I m sending notifications to many users at the same time and this is my code:

        
   public function sendNotifications($title,$body,$image){     
        $SERVER_API_KEY ="my_api_key";
       
        $tokens=DB::select("select * from fcm_token");
     $token_array=[];
        for( $i =0;$i<count($tokens);$i++){
            array_push($token_array,$tokens[$i]->token);
        }
       
            $data = [
    
                "registration_ids" => 
                    $token_array
                ,
        
                "notification" => [
        
                    "title" => $title,
                    
        
                    "body" => $body,
                    "image" => $image,
        
                    "sound"=> "default" // required for sound on ios
        
                ],
        
            ];
            $dataString = json_encode($data);
        
            $headers = [
        
                'Authorization: key=' . $SERVER_API_KEY,
        
                'Content-Type: application/json',
        
            ];
        
            $ch = curl_init();
        
            curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
        
            curl_setopt($ch, CURLOPT_POST, true);
        
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
            curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
        
            $response = curl_exec($ch);
            curl_close($ch);

        

       
    
    }

this function is executed after a form is filled by the admin who should give the title body and image to the back-end. after that, he must wait for the task to be complete (to make sure that everyone got the notification ). it was working like a charm before but right now after a long loading time, the server responds with 504 timeout. I know that I could have queued but would that solve the problem? wouldn't the Queue be stopped also by the same error? I m hosting the app in shared hosting (lws) and my max execution time is 60 seconds and can't increase it without paying more but I want to be sure where the problem resides.

I tried to test it with just 100 users, and it is working like a charm, but I need to send it to more than that since my users are more than 4000.

tarik said
  • 21
  • 4

1 Answers1

1

Google clound messaging supports sending to 1000 tokens at once.

$tokens=array_chunk($all_tokens,1000);

foreach($tokens as $thousand_tokens){
     send_notification($thousand_tokens, $request->title.' Video added', 'New Video added in '.$cat->category->name, $cat->image_url, $data);
 }

Above one is sample code. You can use that to modify your code.

function send_notification($tokens, $message, $description, $image, $data)
{
    try{
        $token_ids = array($tokens);
        $apiKey = env('FCM_KEY');
        $url = 'https://fcm.googleapis.com/fcm/send';

        $msg = 
        array(
            "registration_ids"=> $tokens,
            "collapseKey"=> "com.notification",
            "data"=> $data,
            "notification"=> array(
                "android"=> array(
                    "channelId"=> "Notifications-Channel",
                    "imageUrl"=> $image,
                    "sound"=> "sample.mp3"
                ),
                "sound"=> "sample.mp3",
                "channelId"=> "Notifications-Channel",
                "android_channel_id"=> "Notifications-Channel",
                "body"=> $description,
                "title"=> $message
            )
        );
        
            define("GOOGLE_API_KEY", $apiKey); 
            $headers = array(
                'Authorization: key='.$apiKey,
                '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($msg));
        $result = curl_exec($ch);
            if ($result === FALSE) {
                die('Curl failed: ' . curl_error($ch));
            }
            curl_close($ch);
            return $result;
        }catch(Exception $e){
            return 'at exception '.$e->getLine();
            die('Error: '.$e->getMessage());
        }
}
Vishnu
  • 158
  • 9