![](/img/trans.png)
[英]DOMDocument::schemaValidate() exceeds the allowed maximum length of '255'
[英]{"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.