簡體   English   中英

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

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

當我想通過 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);
                }

在這部分

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

長話短說,如果以下不起作用,請嘗試使用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/ 如何創建 PHP cron 作業?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM