简体   繁体   English

Laravel 中的批量用户通知

[英]Batch user notification in Laravel

I have an application created using Laravel.我有一个使用 Laravel 创建的应用程序。 There are times that I need to send notification (via Database) to my users.有时我需要(通过数据库)向我的用户发送通知。

The problem: Whenever I said notification to all my users (~15k) my app stops working.问题:每当我向所有用户(~15k)说通知时,我的应用程序就会停止工作。 Is there any way I can batch this process?有什么办法可以批处理这个过程吗?

I am using a livewire component to send the notification.我正在使用 livewire 组件发送通知。 When I send a notification to specific users, there's no problem with it.当我向特定用户发送通知时,它没有问题。 However, when I send the notification to all users, my app freezes.但是,当我向所有用户发送通知时,我的应用程序会冻结。

// livewire component
Notification::send($users, new \App\Notifications\SendNotification($this->title, $this->message));

This is my SendNotification.php这是我的 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,
        ];
    }
}

Maybe you can try this也许你可以试试这个

$when = now()->addSecond();
\Notification::send($users, (new \App\Notifications\SendNotification($this->title, $this->message))->delay($when));

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

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