php - Laravel 中的批量用户通知
问题描述
我有一个使用 Laravel 创建的应用程序。有时我需要向我的用户发送通知(通过数据库)。
问题:每当我向所有用户(~15k)说通知时,我的应用程序就会停止工作。有什么办法可以批处理这个过程吗?
我正在使用 livewire 组件发送通知。当我向特定用户发送通知时,它没有问题。但是,当我向所有用户发送通知时,我的应用程序会冻结。
// livewire component
Notification::send($users, new \App\Notifications\SendNotification($this->title, $this->message));
这是我的 SendNotification.php
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class SendNotification extends Notification implements ShouldQueue
{
use Queueable;
protected $title;
protected $message;
/**
* Create a new notification instance.
*
* @param $title
* @param $message
*/
public function __construct($title, $message)
{
$this->title = $title;
$this->message = $message;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
return [
'title' => $this->title,
'message' => $this->message,
];
}
}
解决方案
也许你可以试试这个
$when = now()->addSecond();
\Notification::send($users, (new \App\Notifications\SendNotification($this->title, $this->message))->delay($when));
推荐阅读
- angular - 为什么在ngOnInit中调用函数会出错?
- angular - 在路由器上触发事件。在离子中导航
- php - Laravel 高阶消息意外结果
- c# - 使用从 IFilterProvider 到 NinjectFilterProvider 的绑定激活 IFilterProvider 时出错
- java - 如何用 AND 定义 IF 语句
- sql - 如何将日期数据“转置”到 SQL 中的列中?
- php - 在 Laravel 中生成统计信息
- azure - 如何使用信任库在 azure devops 中创建 sonarqube 任务
- python - 如何在二维数组中创建来自 2 个不同数组的所有组合
- image - 计算屏幕覆盖率