1

i am getting this when i want to send push notifications to all users through firebase, I chunked array but getting same error. please help

 $noti_title = $request->notif_title;
        $noti_body = $request->notif_body;
        if ($request->notif_to == 1) {

            $all_users  = \App\User::orderBy('id','desc')->pluck('id')->toArray();
            $all_user = array_chunk($all_users, 600);
            $all_firebase_tokens = [];
            foreach($all_user as $numbers){
                foreach($numbers as $number){                   
                    array_push($all_firebase_tokens, \App\UserDeviceId::WhereIn('user_id',[$number])->pluck('firebase_token')->toArray());
                }
            }
                if(count($all_firebase_tokens) > 0) {
                    $message = [
                        "registration_ids" => $all_firebase_tokens,
                        "priority" => 'high',
                        "sound" => 'default',
                        "badge" => '1',
                        "data" =>
                        [
                            "title" => $noti_title,
                            "body"  => $noti_body,
                            "type" => 'Admin_notification',
                        ],
                        "notification" =>
                        [
                            "title" => $noti_title,
                            "body"  => $noti_body,
                            "type" => 'Admin_notification',
                        ]
                    ];
                  return \App\PushNotification::send($message);
                }

2 Answers2

0

In this part

        $all_firebase_tokens = [];
        foreach($all_user as $numbers){
            foreach($numbers as $number){                   
                array_push($all_firebase_tokens, \App\UserDeviceId::WhereIn('user_id',[$number])->pluck('firebase_token')->toArray());
            }
        }

You still have more when 600

Because $all_firebase_tokens = []; placed before nested foreach

Actually you can try something like this

foreach($all_user as $numbers){
    
    $all_firebase_tokens = [];
    foreach($numbers as $number){                   
        array_push($all_firebase_tokens, \App\UserDeviceId::WhereIn('user_id',[$number])->pluck('firebase_token')->toArray());
    }

    if(count($all_firebase_tokens) > 0) {
        $message = [
           "registration_ids" => $all_firebase_tokens,
           "priority" => 'high',
           "sound" => 'default',
           "badge" => '1',
           "data" =>
             [
               "title" => $noti_title,
               "body"  => $noti_body,
               "type" => 'Admin_notification',
             ],
               "notification" =>
             [
                "title" => $noti_title,
                "body"  => $noti_body,
                "type" => 'Admin_notification',
             ]
         ];
      return \App\PushNotification::send($message);
   }
}
Vitalii Isaev
  • 63
  • 1
  • 6
0

Long story short, if below does not work, try using a cron job.

Possible issue

In the code you limit to 600 users, which is less than 1000.

$all_user = array_chunk($all_users, 600);

But later your code iterates:

foreach($all_user as $numbers){
    foreach($numbers as $number){ 

Meaning users can have multiple tokens.

If each user has just 2 numbers, we reach 600*2 = 1200 ;-)

Solution

To see if I am right, simply try:

$all_user = array_chunk($all_users, 1);

Instead of:

$all_user = array_chunk($all_users, 600);

And improve your logic to chunk tokens, not users.

Alternative

Chances are that above solution does not work, if so read below.

I am not sure about Firebase, but some API have time limits, I mean, 1000 per second or even per day (instead of per request).

In such cases you would need to use a cron job.

See also: stackoverflow.com/How to create PHP cron job?

Top-Master
  • 7,611
  • 5
  • 39
  • 71