简体   繁体   English

{"status":true,"message":"批量消息数 (1016) 超过允许的最大值 (1000)\n"}

[英]{"status":true,"message":"Number of messages on bulk (1016) exceeds maximum allowed (1000)\n"}

i am getting this when i want to send push notifications to all users through firebase, I chunked array but getting same error.当我想通过 firebase 向所有用户发送推送通知时,我得到了这个,我对数组进行了分块但得到了同样的错误。 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);
                }

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 600的时候你还有更多

Because $all_firebase_tokens = [];因为$all_firebase_tokens = []; placed before nested foreach放置在嵌套的 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);
   }
}

Long story short, if below does not work, try using a cron job.长话短说,如果以下不起作用,请尝试使用cron作业。

Possible issue可能的问题

In the code you limit to 600 users, which is less than 1000 .在代码中,您限制为600用户,小于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 ;-)如果每个用户只有2号码,我们达到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).我不确定 Firebase,但有些 API 有时间限制,我的意思是,每秒甚至每天 1000 个(而不是每个请求)。

In such cases you would need to use a cron job.在这种情况下,您需要使用cron作业。

See also: stackoverflow.com/ How to create PHP cron job?另请参阅:stackoverflow.com/ 如何创建 PHP cron 作业?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM