【问题标题】:{"status":true,"message":"Number of messages on bulk (1016) exceeds maximum allowed (1000)\n"}{"status":true,"message":"批量消息数 (1016) 超过允许的最大值 (1000)\n"}
【发布时间】:2022-01-24 09:46:09
【问题描述】:

当我想通过 firebase 向所有用户发送推送通知时,我得到了这个,我对数组进行了分块但得到了同样的错误。请帮忙

 $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);
                }

【问题讨论】:

  • 将您的订阅者列表分成少于 1000 个(可能是 999 个)的块,因为它不支持单个实例超过 1000 个。
  • 先生,请检查我的代码,我已经将数组分块为 600

标签: php arrays laravel firebase push-notification


【解决方案1】:

在这部分

        $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());
            }
        }

600 的时候你还有更多

因为$all_firebase_tokens = [];放在嵌套foreach之前

其实你可以试试这样的

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);
   }
}

【讨论】:

    【解决方案2】:

    长话短说,如果以下方法不起作用,请尝试使用 cron 工作。

    可能的问题

    在代码中您限制为600 用户,小于1000

    $all_user = array_chunk($all_users, 600);
    

    但稍后您的代码会迭代:

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

    意味着用户可以拥有多个令牌。

    如果每个用户只有2 号码,我们会到达600*2 = 1200 ;-)

    解决方案

    要看看我是否正确,只需尝试:

    $all_user = array_chunk($all_users, 1);
    

    代替:

    $all_user = array_chunk($all_users, 600);
    

    并改进你的逻辑来分块令牌,而不是用户。

    另类

    上述解决方案可能不起作用,如果是这样,请阅读下文。

    我不确定 Firebase,但有些 API 有时间限制, 我的意思是,每秒甚至每天 1000 个(而不是每个请求)。

    在这种情况下,您需要使用 cron 作业。

    另请参阅:stackoverflow.com/How to create PHP cron job?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      • 2015-05-20
      • 2021-12-13
      • 1970-01-01
      • 2021-05-19
      • 1970-01-01
      相关资源
      最近更新 更多