W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
注:使用通知隊列前需要配置隊列并 開啟一個隊列任務(wù)。
發(fā)送通知可能是耗時的,尤其是通道需要調(diào)用額外的 API 來傳輸通知。為了加速應(yīng)用的響應(yīng)時間,可以將通知推送到隊列中異步發(fā)送,而要實現(xiàn)推送通知到隊列,可以讓對應(yīng)通知類實現(xiàn) ShouldQueue
接口并使用 Queueable
trait。如果通知類是通過 make:notification
命令生成的,那么該接口和 trait 已經(jīng)默認導(dǎo)入,你可以快速將它們添加到通知類:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
class InvoicePaid extends Notification implements ShouldQueue
{
use Queueable;
// ...
}
ShouldQueue
接口被添加到通知類以后,你可以像之前一樣正常發(fā)送通知,Laravel 檢測到 ShouldQueue
接口后會自動將通知的發(fā)送添加到隊列:
$user->notify(new InvoicePaid($invoice));
如果你想要延遲通知的發(fā)送,可以在通知實例后加上 delay
方法:
$when = now()->addMinutes(10);
$user->notify((new InvoicePaid($invoice))->delay($when));
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: