通知
简介
除了支持发送邮件,Laravel 还支持通过各种投递渠道发送通知,包括邮件、短信(通过 Vonage,前身为 Nexmo)和 Slack。此外,社区还创建了各种通知渠道,可通过数十种不同渠道发送通知!通知还可以存储在数据库中,以便在 Web 界面中显示。
通常,通知应该是简短的信息性消息,通知用户应用中发生的某些事情。例如,如果你正在编写计费应用,可以通过邮件和短信渠道向用户发送「发票已支付」通知。
生成通知
在 Laravel 中,每个通知都由一个单独的类表示,通常存储在 app/Notifications 目录中。如果应用中没有此目录不必担心,运行 make:notification Artisan 命令时会自动创建:
php artisan make:notification InvoicePaid
此命令会在 app/Notifications 目录中放置一个新的通知类。每个通知类包含一个 via() 方法和多个消息构建方法(如 toMail() 或 toDatabase()),将通知转换为针对特定渠道定制的消息。
发送通知
使用 Notifiable Trait
通知可以通过两种方式发送:使用 Notifiable trait 的 notify() 方法或使用 Notification 门面。Notifiable trait 默认包含在应用的 App\Models\User 模型上:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
}
此 trait 提供的 notify() 方法期望接收一个通知实例:
use App\Notifications\InvoicePaid;
$user->notify(new InvoicePaid($invoice));
信息
记住,你可以在任何模型上使用 Notifiable trait。不限于只在 User 模型上使用。
使用 Notification 门面
或者,可以通过 Notification 门面发送通知。当需要向多个可通知实体(如用户集合)发送通知时,此方式很有用。使用门面发送通知时,将所有可通知实体和通知实例传递给 send() 方法:
use Illuminate\Support\Facades\Notification;
Notification::send($users, new InvoicePaid($invoice));
还可以使用 sendNow() 方法立即发送通知。即使通知实现了 ShouldQueue 接口,此方法也会立即发送通知:
Notification::sendNow($developers, new DeploymentCompleted($deployment));
指定投递渠道
每个通知类都有一个 via() 方法,决定通知将在哪些渠道上投递。通知可以在 mail、database、broadcast、vonage 和 slack 渠道上发送。
信息
如果想使用其他投递渠道(如 Telegram 或 Pusher),请查看社区驱动的 Laravel Notification Channels 网站。
via() 方法接收一个 $notifiable 实例,即通知发送到的类的实例。可以使用 $notifiable 来确定通知应在哪些渠道上投递:
/**
* 获取通知的投递渠道。
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return $notifiable->prefers_sms ? ['vonage'] : ['mail', 'database'];
}
队列通知
注意
在队列通知之前,应配置队列并启动 worker。
发送通知可能需要时间,特别是当渠道需要调用外部 API 来投递通知时。为加快应用的响应时间,可以通过在类上添加 ShouldQueue 接口和 Queueable trait 来让通知被队列处理。使用 make:notification 命令生成的所有通知都已导入了此接口和 trait,因此可以立即将它们添加到通知类中:
<?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 接口后,可以像平常一样发送通知。Laravel 会检测到类上的 ShouldQueue 接口并自动将通知的投递排队:
$user->notify(new InvoicePaid($invoice));
队列通知时,会为每个收件人和渠道组合创建一个队列任务。例如,如果通知有 3 个收件人和 2 个渠道,将会向队列分派 6 个任务。
延迟通知
如果想延迟通知的投递,可以在通知实例化时链式调用 delay() 方法:
$delay = now()->plus(minutes: 10);
$user->notify((new InvoicePaid($invoice))->delay($delay));
可以向 delay() 方法传递数组来为特定渠道指定延迟时间:
$user->notify((new InvoicePaid($invoice))->delay([
'mail' => now()->plus(minutes: 5),
'sms' => now()->plus(minutes: 10),
]));
或者,可以在通知类本身上定义 withDelay() 方法。该方法应返回渠道名称和延迟值的数组:
/**
* 确定通知的投递延迟。
*
* @return array<string, \Illuminate\Support\Carbon>
*/
public function withDelay(object $notifiable): array
{
return [
'mail' => now()->plus(minutes: 5),
'sms' => now()->plus(minutes: 10),
];
}
自定义通知队列连接
默认情况下,队列通知会使用应用的默认队列连接进行排队。如果想为特定通知指定不同的连接,可以在通知的构造函数中调用 onConnection() 方法:
<?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;
/**
* 创建新通知实例。
*/
public function __construct()
{
$this->onConnection('redis');
}
}
或者,如果想为通知支持的每个通知渠道指定特定的队列连接,可以在通知上定义 viaConnections() 方法。该方法应返回渠道名称/队列连接名称对的数组:
/**
* 确定每个通知渠道应使用哪个连接。
*
* @return array<string, string>
*/
public function viaConnections(): array
{
return [
'mail' => 'redis',
'database' => 'sync',
];
}
自定义通知渠道队列
如果想为通知支持的每个通知渠道指定特定的队列,可以在通知上定义 viaQueues() 方法。该方法应返回渠道名称/队列名称对的数组:
/**
* 确定每个通知渠道应使用哪个队列。
*
* @return array<string, string>
*/
public function viaQueues(): array
{
return [
'mail' => 'mail-queue',
'slack' => 'slack-queue',
];
}
自定义队列通知任务属性
可以通过在通知类上定义队列属性来自定义底层队列任务的行为。这些属性将被发送通知的队列任务继承:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Queue\Attributes\MaxExceptions;
use Illuminate\Queue\Attributes\Timeout;
use Illuminate\Queue\Attributes\Tries;
#[Tries(5)]
#[Timeout(120)]
#[MaxExceptions(3)]
class InvoicePaid extends Notification implements ShouldQueue
{
use Queueable;
// ...
}
如果想通过加密确保队列通知数据的隐私和完整性,可以在通知类上添加 ShouldBeEncrypted 接口:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
class InvoicePaid extends Notification implements ShouldQueue, ShouldBeEncrypted
{
use Queueable;
// ...
}
除了在通知类上直接定义这些属性外,还可以定义 backoff() 和 retryUntil() 方法来指定队列通知任务的退避策略和重试超时:
use DateTime;
/**
* 计算重试通知前等待的秒数。
*/
public function backoff(): int
{
return 3;
}
/**
* 确定通知应超时的时间。
*/
public function retryUntil(): DateTime
{
return now()->plus(minutes: 5);
}
信息
有关这些任务属性和方法的更多信息,请查阅队列任务的文档。
队列通知中间件
队列通知可以像队列任务一样定义中间件。首先,在通知类上定义 middleware() 方法。该方法接收 $notifiable 和 $channel 变量,允许你根据通知的目的地自定义返回的中间件:
use Illuminate\Queue\Middleware\RateLimited;
/**
* 获取通知任务应通过的中间件。
*
* @return array<int, object>
*/
public function middleware(object $notifiable, string $channel)
{
return match ($channel) {
'mail' => [new RateLimited('postmark')],
'slack' => [new RateLimited('slack')],
default => [],
};
}
队列通知与数据库事务
当队列通知在数据库事务内分派时,它们可能在数据库事务提交之前就被队列处理。发生这种情况时,在数据库事务期间对模型或数据库记录所做的任何更新可能尚未反映在数据库中。此外,在事务内创建的任何模型或数据库记录可能不存在于数据库中。如果你的通知依赖于这些模型,发送队列通知的任务被处理时可能会发生意外错误。
如果队列连接的 after_commit 配置选项设置为 false,仍可以通过在发送通知时调用 afterCommit() 方法来指示特定队列通知应在所有打开的数据库事务提交后再分派:
use App\Notifications\InvoicePaid;
$user->notify((new InvoicePaid($invoice))->afterCommit());
或者,可以在通知的构造函数中调用 afterCommit() 方法:
<?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;
/**
* 创建新通知实例。
*/
public function __construct()
{
$this->afterCommit();
}
}
信息
要了解更多关于解决这些问题的信息,请查阅队列任务与数据库事务的文档。
确定队列通知是否应发送
队列通知被分派到队列进行后台处理后,通常会由队列 worker 接收并发送给目标收件人。
但是,如果想在队列 worker 处理后最终确定队列通知是否应发送,可以在通知类上定义 shouldSend() 方法。如果此方法返回 false,通知将不会被发送:
/**
* 确定通知是否应发送。
*/
public function shouldSend(object $notifiable, string $channel): bool
{
return $this->invoice->isPaid();
}
发送通知后
如果想在通知发送后执行代码,可以在通知类上定义 afterSending() 方法。此方法接收可通知实体、渠道名称和渠道的响应:
/**
* 在通知发送后处理。
*/
public function afterSending(object $notifiable, string $channel, mixed $response): void
{
// ...
}
按需通知
有时你可能需要向未存储为应用「用户」的人发送通知。使用 Notification 门面的 route() 方法,可以在发送通知前指定临时通知路由信息:
use Illuminate\Broadcasting\Channel;
use Illuminate\Support\Facades\Notification;
Notification::route('mail', 'taylor@example.com')
->route('vonage', '5555555555')
->route('slack', '#slack-channel')
->route('broadcast', [new Channel('channel-name')])
->notify(new InvoicePaid($invoice));
如果想在向 mail 路由发送按需通知时提供收件人姓名,可以提供一个数组,以电子邮件地址为键,以姓名为值的第一个元素:
Notification::route('mail', [
'barrett@example.com' => 'Barrett Blair',
])->notify(new InvoicePaid($invoice));
使用 routes() 方法,可以一次为多个通知渠道提供临时路由信息:
Notification::routes([
'mail' => ['barrett@example.com' => 'Barrett Blair'],
'vonage' => '5555555555',
])->notify(new InvoicePaid($invoice));
邮件通知
格式化邮件消息
如果通知支持作为电子邮件发送,应在通知类上定义 toMail() 方法。此方法接收一个 $notifiable 实体,应返回一个 Illuminate\Notifications\Messages\MailMessage 实例。
MailMessage 类包含几个简单的方法来帮助你构建事务邮件消息。邮件消息可以包含文本行和「行动号召」。来看一个 toMail() 方法的示例:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
$url = url('/invoice/'.$this->invoice->id);
return (new MailMessage)
->greeting('Hello!')
->line('One of your invoices has been paid!')
->lineIf($this->amount > 0, "Amount paid: {$this->amount}")
->action('View Invoice', $url)
->line('Thank you for using our application!');
}
信息
注意我们在 toMail() 方法中使用了 $this->invoice->id。你可以将通知生成消息所需的任何数据传递到通知的构造函数中。
在此示例中,我们注册了问候语、一行文本、行动号召,然后是另一行文本。MailMessage 对象提供的这些方法使得格式化小型事务邮件变得简单快速。邮件渠道随后会将消息组件转换为美观、响应式的 HTML 邮件模板,并附带纯文本对应版本。
信息
发送邮件通知时,请确保在 config/app.php 配置文件中设置了 name 配置选项。此值将用于邮件通知消息的页眉和页脚。
错误消息
某些通知告知用户错误,例如发票支付失败。可以通过在构建消息时调用 error() 方法来指示邮件消息与错误相关。在邮件消息上使用 error() 方法时,行动号召按钮将是红色而不是黑色:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->error()
->subject('Invoice Payment Failed')
->line('...');
}
其他邮件通知格式化选项
除了在通知类中定义「行」文本外,还可以使用 view() 方法指定应用于渲染通知邮件的自定义模板:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)->view(
'mail.invoice.paid', ['invoice' => $this->invoice]
);
}
可以通过将视图名作为数组的第二个元素传递给 view() 方法来指定邮件消息的纯文本视图:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)->view(
['mail.invoice.paid', 'mail.invoice.paid-text'],
['invoice' => $this->invoice]
);
}
或者,如果消息只有纯文本视图,可以使用 text() 方法:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)->text(
'mail.invoice.paid-text', ['invoice' => $this->invoice]
);
}
自定义发件人
默认情况下,邮件的发件人/发件地址在 config/mail.php 配置文件中定义。但可以使用 from() 方法为特定通知指定发件地址:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->from('barrett@example.com', 'Barrett Blair')
->line('...');
}
自定义收件人
通过 mail 渠道发送通知时,通知系统会自动在可通知实体上查找 email 属性。可以通过在可通知实体上定义 routeNotificationForMail() 方法来自定义用于投递通知的电子邮件地址:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
class User extends Authenticatable
{
use Notifiable;
/**
* 为邮件渠道路由通知。
*
* @return array<string, string>|string
*/
public function routeNotificationForMail(Notification $notification): array|string
{
// 仅返回电子邮件地址...
return $this->email_address;
// 返回电子邮件地址和姓名...
return [$this->email_address => $this->name];
}
}
自定义主题
默认情况下,邮件的主题是通知类名格式化为「标题大小写」。因此,如果通知类名为 InvoicePaid,邮件主题将是 Invoice Paid。如果想为消息指定不同的主题,可以在构建消息时调用 subject() 方法:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject('Notification Subject')
->line('...');
}
自定义邮件器
默认情况下,邮件通知会使用 config/mail.php 配置文件中定义的默认邮件器发送。但可以在构建消息时调用 mailer() 方法在运行时指定不同的邮件器:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->mailer('postmark')
->line('...');
}
自定义模板
可以通过发布通知包的资源来修改邮件通知使用的 HTML 和纯文本模板。运行此命令后,邮件通知模板将位于 resources/views/vendor/notifications 目录:
php artisan vendor:publish --tag=laravel-notifications
附件
要向邮件通知添加附件,在构建消息时使用 attach() 方法。attach() 方法接受文件的绝对路径作为第一个参数:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->greeting('Hello!')
->attach('/path/to/file');
}
向消息添加附件时,还可以通过将 array 作为第二个参数传递给 attach() 方法来指定显示名称和/或 MIME 类型:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->greeting('Hello!')
->attach('/path/to/file', [
'as' => 'name.pdf',
'mime' => 'application/pdf',
]);
}
需要时,可以使用 attachMany() 方法向消息附加多个文件:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->greeting('Hello!')
->attachMany([
'/path/to/forge.svg',
'/path/to/vapor.svg' => [
'as' => 'Logo.svg',
'mime' => 'image/svg+xml',
],
]);
}
可以使用 attachFromStorageDisk() 方法附加存在于特定文件系统磁盘上的文件。此方法接受磁盘名和文件在该磁盘上的路径:
use App\Mail\InvoicePaid as InvoicePaidMailable;
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): Mailable
{
return (new InvoicePaidMailable($this->invoice))
->to($notifiable->email)
->attachFromStorageDisk('s3', '/path/to/file', 'invoice.pdf', [
'mime' => 'application/pdf',
]);
}
原始数据附件
attachData() 方法可用于将原始字节字符串作为附件附加。调用 attachData() 方法时,应提供应分配给附件的文件名:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->greeting('Hello!')
->attachData($this->pdf, 'name.pdf', [
'mime' => 'application/pdf',
]);
}
添加标签和元数据
一些第三方邮件提供商(如 Mailgun 和 Postmark)支持消息「标签」和「元数据」,可用于分组和跟踪应用发送的邮件。可以通过 tag() 和 metadata() 方法向邮件消息添加标签和元数据:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->greeting('Comment Upvoted!')
->tag('upvote')
->metadata('comment_id', $this->comment->id);
}
如果你的应用使用 Mailgun 驱动,可以查阅 Mailgun 文档了解更多关于标签和元数据的信息。同样,也可以查阅 Postmark 文档了解更多关于其标签和元数据支持的信息。
如果你的应用使用 Amazon SES 发送邮件,应使用 metadata() 方法向消息附加 SES「标签」。
自定义 Symfony Message
MailMessage 类的 withSymfonyMessage() 方法允许你注册一个闭包,在发送消息前用 Symfony Message 实例调用。这让你有机会在消息投递前深度自定义消息:
use Symfony\Component\Mime\Email;
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->withSymfonyMessage(function (Email $message) {
$message->getHeaders()->addTextHeader(
'Custom-Header', 'Header Value'
);
});
}
使用 Mailable
需要时,可以从通知的 toMail() 方法返回完整的 mailable 对象。返回 Mailable 而非 MailMessage 时,需要使用 mailable 对象的 to() 方法指定消息收件人:
use App\Mail\InvoicePaid as InvoicePaidMailable;
use Illuminate\Mail\Mailable;
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): Mailable
{
return (new InvoicePaidMailable($this->invoice))
->to($notifiable->email);
}
Mailable 与按需通知
如果发送的是按需通知,传递给 toMail() 方法的 $notifiable 实例将是 Illuminate\Notifications\AnonymousNotifiable 的实例,它提供了 routeNotificationFor() 方法,可用于检索按需通知应发送到的电子邮件地址:
use App\Mail\InvoicePaid as InvoicePaidMailable;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Mail\Mailable;
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): Mailable
{
$address = $notifiable instanceof AnonymousNotifiable
? $notifiable->routeNotificationFor('mail')
: $notifiable->email;
return (new InvoicePaidMailable($this->invoice))
->to($address);
}
预览邮件通知
设计邮件通知模板时,像典型 Blade 模板一样在浏览器中快速预览渲染后的邮件消息非常方便。为此,Laravel 允许你直接从路由闭包或控制器返回邮件通知生成的任何邮件消息。返回 MailMessage 时,它将被渲染并显示在浏览器中,让你无需发送到实际电子邮件地址即可快速预览其设计:
use App\Models\Invoice;
use App\Notifications\InvoicePaid;
Route::get('/notification', function () {
$invoice = Invoice::find(1);
return (new InvoicePaid($invoice))
->toMail($invoice->user);
});
Markdown 邮件通知
Markdown 邮件通知允许你利用邮件通知的预构建模板,同时给你更多自由来编写更长、更自定义的消息。由于消息使用 Markdown 编写,Laravel 能够为消息渲染美观、响应式的 HTML 模板,同时自动生成纯文本对应版本。
生成消息
要生成带有对应 Markdown 模板的通知,可以使用 make:notification Artisan 命令的 --markdown 选项:
php artisan make:notification InvoicePaid --markdown=mail.invoice.paid
与所有其他邮件通知一样,使用 Markdown 模板的通知应在其通知类上定义 toMail() 方法。但不是使用 line() 和 action() 方法来构建通知,而是使用 markdown() 方法指定应使用的 Markdown 模板名称。希望在模板中可用的数据数组可作为方法的第二个参数传递:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
$url = url('/invoice/'.$this->invoice->id);
return (new MailMessage)
->subject('Invoice Paid')
->markdown('mail.invoice.paid', ['url' => $url]);
}
编写消息
Markdown 邮件通知使用 Blade 组件和 Markdown 语法的组合,让你在利用 Laravel 预构建的通知组件的同时轻松构建通知:
<x-mail::message>
# Invoice Paid
Your invoice has been paid!
<x-mail::button :url="$url">
View Invoice
</x-mail::button>
Thanks,<br>
{{ config('app.name') }}
</x-mail::message>
信息
编写 Markdown 邮件时不要使用过多缩进。根据 Markdown 标准,Markdown 解析器会将缩进内容渲染为代码块。
按钮组件
按钮组件渲染一个居中的按钮链接。该组件接受两个参数:url 和可选的 color。支持的颜色有 primary、green 和 red。可以在通知中添加任意数量的按钮组件:
<x-mail::button :url="$url" color="green">
View Invoice
</x-mail::button>
面板组件
面板组件将给定文本块渲染在背景色与通知其余部分略有不同的面板中。这让你可以吸引对特定文本块的注意:
<x-mail::panel>
This is the panel content.
</x-mail::panel>
表格组件
表格组件允许你将 Markdown 表格转换为 HTML 表格。该组件接受 Markdown 表格作为其内容。支持使用默认 Markdown 表格对齐语法进行列对齐:
<x-mail::table>
| Laravel | Table | Example |
| ------------- | :-----------: | ------------: |
| Col 2 is | Centered | $10 |
| Col 3 is | Right-Aligned | $20 |
</x-mail::table>
自定义组件
你可以将所有 Markdown 通知组件导出到自己的应用中进行自定义。要导出组件,使用 vendor:publish Artisan 命令发布 laravel-mail 资产标签:
php artisan vendor:publish --tag=laravel-mail
此命令将 Markdown 邮件组件发布到 resources/views/vendor/mail 目录。mail 目录将包含 html 和 text 目录,每个目录分别包含每个可用组件的对应表示。你可以自由自定义这些组件。
自定义 CSS
导出组件后,resources/views/vendor/mail/html/themes 目录将包含一个 default.css 文件。你可以自定义此文件中的 CSS,你的样式将自动内联到 Markdown 通知的 HTML 表示中。
如果想为 Laravel 的 Markdown 组件构建全新主题,可以在 html/themes 目录中放置一个 CSS 文件。命名并保存 CSS 文件后,更新 mail 配置文件中的 theme 选项以匹配新主题的名称。
要为单个通知自定义主题,可以在构建通知的邮件消息时调用 theme() 方法。theme() 方法接受发送通知时应使用的主题名称:
/**
* 获取通知的邮件表示。
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->theme('invoice')
->subject('Invoice Paid')
->markdown('mail.invoice.paid', ['url' => $url]);
}
数据库通知
前提条件
database 通知渠道将通知信息存储在数据库表中。此表将包含通知类型以及描述通知的 JSON 数据结构等信息。
你可以查询该表以在应用的用户界面中显示通知。但在此之前,你需要创建一个数据库表来保存通知。可以使用 make:notifications-table 命令生成具有正确表结构的迁移:
php artisan make:notifications-table
php artisan migrate
信息
如果你的可通知模型使用 UUID 或 ULID 主键,应在通知表迁移中将 morphs 方法替换为 uuidMorphs 或 ulidMorphs。
格式化数据库通知
如果通知支持存储在数据库表中,应在通知类上定义 toDatabase() 或 toArray() 方法。此方法接收一个 $notifiable 实体,应返回一个普通 PHP 数组。返回的数组将被编码为 JSON 并存储在 notifications 表的 data 列中。来看一个 toArray() 方法的示例:
/**
* 获取通知的数组表示。
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'invoice_id' => $this->invoice->id,
'amount' => $this->invoice->amount,
];
}
当通知存储在应用的数据库中时,type 列默认设置为通知的类名,read_at 列为 null。但可以通过在通知类中定义 databaseType() 和 initialDatabaseReadAtValue() 方法来自定义此行为:
use Illuminate\Support\Carbon;
/**
* 获取通知的数据库类型。
*/
public function databaseType(object $notifiable): string
{
return 'invoice-paid';
}
/**
* 获取 "read_at" 列的初始值。
*/
public function initialDatabaseReadAtValue(): ?Carbon
{
return null;
}
toDatabase vs toArray
toArray() 方法也用于 broadcast 渠道,以确定要广播到 JavaScript 前端的数据。如果想为 database 和 broadcast 渠道设置不同的数组表示,应定义 toDatabase() 方法而不是 toArray() 方法。
访问通知
通知存储在数据库中后,你需要一种便捷的方式从可通知实体中访问它们。Illuminate\Notifications\Notifiable trait(包含在 Laravel 默认的 App\Models\User 模型上)包含一个 notifications Eloquent 关系,返回该实体的通知。要获取通知,可以像访问任何其他 Eloquent 关系一样访问此方法。默认情况下,通知按 created_at 时间戳排序,最新通知在集合开头:
$user = App\Models\User::find(1);
foreach ($user->notifications as $notification) {
echo $notification->type;
}
如果只想获取「未读」通知,可以使用 unreadNotifications 关系。同样,这些通知按 created_at 时间戳排序,最新通知在集合开头:
$user = App\Models\User::find(1);
foreach ($user->unreadNotifications as $notification) {
echo $notification->type;
}
如果只想获取「已读」通知,可以使用 readNotifications 关系:
$user = App\Models\User::find(1);
foreach ($user->readNotifications as $notification) {
echo $notification->type;
}
信息
要从 JavaScript 客户端访问通知,应为应用定义一个通知控制器,返回可通知实体(如当前用户)的通知。然后可以从 JavaScript 客户端向该控制器的 URL 发起 HTTP 请求。
标记通知为已读
通常,当用户查看通知时,你会想将其标记为「已读」。Illuminate\Notifications\Notifiable trait 提供了 markAsRead() 方法,更新通知数据库记录上的 read_at 列:
$user = App\Models\User::find(1);
foreach ($user->unreadNotifications as $notification) {
$notification->markAsRead();
}
但无需遍历每个通知,可以直接在通知集合上使用 markAsRead() 方法:
$user->unreadNotifications->markAsRead();
还可以使用批量更新查询将所有通知标记为已读,而无需从数据库中检索它们:
$user = App\Models\User::find(1);
$user->unreadNotifications()->update(['read_at' => now()]);
可以 delete() 通知以将其从表中完全移除:
$user->notifications()->delete();
广播通知
前提条件
在广播通知之前,应配置并熟悉 Laravel 的事件广播服务。事件广播提供了一种从 JavaScript 前端响应服务端 Laravel 事件的方式。
格式化广播通知
broadcast 渠道使用 Laravel 的事件广播服务广播通知,让你的 JavaScript 前端实时捕获通知。如果通知支持广播,可以在通知类上定义 toBroadcast() 方法。此方法接收一个 $notifiable 实体,应返回一个 BroadcastMessage 实例。如果 toBroadcast() 方法不存在,将使用 toArray() 方法来收集应广播的数据。返回的数据将被编码为 JSON 并广播到 JavaScript 前端。来看一个 toBroadcast() 方法的示例:
use Illuminate\Notifications\Messages\BroadcastMessage;
/**
* 获取通知的可广播表示。
*/
public function toBroadcast(object $notifiable): BroadcastMessage
{
return new BroadcastMessage([
'invoice_id' => $this->invoice->id,
'amount' => $this->invoice->amount,
]);
}
广播队列配置
所有广播通知都会被队列广播。如果想配置用于排队广播操作的队列连接或队列名称,可以使用 BroadcastMessage 的 onConnection() 和 onQueue() 方法:
return (new BroadcastMessage($data))
->onConnection('sqs')
->onQueue('broadcasts');
自定义通知类型
除了你指定的数据外,所有广播通知还包含一个 type 字段,包含通知的完整类名。如果想自定义通知 type,可以在通知类上定义 broadcastType() 方法:
/**
* 获取正在广播的通知类型。
*/
public function broadcastType(): string
{
return 'broadcast.message';
}
监听通知
通知将使用 {notifiable}.{id} 约定格式的私有频道进行广播。因此,如果向 ID 为 1 的 App\Models\User 实例发送通知,通知将在 App.Models.User.1 私有频道上广播。使用 Laravel Echo 时,可以使用 notification() 方法轻松监听频道上的通知:
Echo.private('App.Models.User.' + userId)
.notification((notification) => {
console.log(notification.type);
});
使用 React、Vue 或 Svelte
Laravel Echo 包含 React、Vue 和 Svelte hooks,使监听通知变得轻松。首先,调用 useEchoNotification hook 来监听通知。useEchoNotification hook 会在使用它的组件卸载时自动离开频道:
import { useEchoNotification } from "@laravel/echo-react";
useEchoNotification(
`App.Models.User.${userId}`,
(notification) => {
console.log(notification.type);
},
);
<script setup lang="ts">
import { useEchoNotification } from "@laravel/echo-vue";
useEchoNotification(
`App.Models.User.${userId}`,
(notification) => {
console.log(notification.type);
},
);
</script>
<script>
import { useEchoNotification } from "@laravel/echo-svelte";
useEchoNotification(
`App.Models.User.${userId}`,
(notification) => {
console.log(notification.type);
},
);
</script>
默认情况下,该 hook 监听所有通知。要指定你想监听的通知类型,可以向 useEchoNotification 提供字符串或类型数组:
import { useEchoNotification } from "@laravel/echo-react";
useEchoNotification(
`App.Models.User.${userId}`,
(notification) => {
console.log(notification.type);
},
'App.Notifications.InvoicePaid',
);
<script setup lang="ts">
import { useEchoNotification } from "@laravel/echo-vue";
useEchoNotification(
`App.Models.User.${userId}`,
(notification) => {
console.log(notification.type);
},
'App.Notifications.InvoicePaid',
);
</script>
<script>
import { useEchoNotification } from "@laravel/echo-svelte";
useEchoNotification(
`App.Models.User.${userId}`,
(notification) => {
console.log(notification.type);
},
'App.Notifications.InvoicePaid',
);
</script>
还可以指定通知负载数据的形状,提供更好的类型安全和编辑便利:
type InvoicePaidNotification = {
invoice_id: number;
created_at: string;
};
useEchoNotification<InvoicePaidNotification>(
`App.Models.User.${userId}`,
(notification) => {
console.log(notification.invoice_id);
console.log(notification.created_at);
console.log(notification.type);
},
'App.Notifications.InvoicePaid',
);
自定义通知频道
如果想自定义实体的广播通知在哪个频道上广播,可以在可通知实体上定义 receivesBroadcastNotificationsOn() 方法:
<?php
namespace App\Models;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* 用户接收通知广播的频道。
*/
public function receivesBroadcastNotificationsOn(): string
{
return 'users.'.$this->id;
}
}
短信通知
前提条件
Laravel 中的短信通知发送由 Vonage(前身为 Nexmo)驱动。通过 Vonage 发送通知之前,需要安装 laravel/vonage-notification-channel 和 guzzlehttp/guzzle 包:
composer require laravel/vonage-notification-channel guzzlehttp/guzzle
该包包含一个配置文件。但无需将此配置文件导出到自己的应用。可以直接使用 VONAGE_KEY 和 VONAGE_SECRET 环境变量来定义 Vonage 公钥和密钥。
定义密钥后,应设置 VONAGE_SMS_FROM 环境变量,定义默认发送短信的电话号码。可以在 Vonage 控制面板中生成此电话号码:
VONAGE_SMS_FROM=15556666666
格式化短信通知
如果通知支持作为短信发送,应在通知类上定义 toVonage() 方法。此方法接收一个 $notifiable 实体,应返回一个 Illuminate\Notifications\Messages\VonageMessage 实例:
use Illuminate\Notifications\Messages\VonageMessage;
/**
* 获取通知的 Vonage / 短信表示。
*/
public function toVonage(object $notifiable): VonageMessage
{
return (new VonageMessage)
->content('Your SMS message content');
}
Unicode 内容
如果短信消息将包含 unicode 字符,在构造 VonageMessage 实例时应调用 unicode() 方法:
use Illuminate\Notifications\Messages\VonageMessage;
/**
* 获取通知的 Vonage / 短信表示。
*/
public function toVonage(object $notifiable): VonageMessage
{
return (new VonageMessage)
->content('Your unicode message')
->unicode();
}
自定义「发件人」号码
如果想从与 VONAGE_SMS_FROM 环境变量指定的电话号码不同的号码发送某些通知,可以在 VonageMessage 实例上调用 from() 方法:
use Illuminate\Notifications\Messages\VonageMessage;
/**
* 获取通知的 Vonage / 短信表示。
*/
public function toVonage(object $notifiable): VonageMessage
{
return (new VonageMessage)
->content('Your SMS message content')
->from('15554443333');
}
添加客户端引用
如果想按用户、团队或客户跟踪成本,可以向通知添加「客户端引用」。Vonage 允许使用此客户端引用生成报告,以便更好地了解特定客户的短信使用情况。客户端引用可以是最多 40 个字符的任意字符串:
use Illuminate\Notifications\Messages\VonageMessage;
/**
* 获取通知的 Vonage / 短信表示。
*/
public function toVonage(object $notifiable): VonageMessage
{
return (new VonageMessage)
->clientReference((string) $notifiable->id)
->content('Your SMS message content');
}
路由短信通知
要将 Vonage 通知路由到正确的电话号码,在可通知实体上定义 routeNotificationForVonage() 方法:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
class User extends Authenticatable
{
use Notifiable;
/**
* 为 Vonage 渠道路由通知。
*/
public function routeNotificationForVonage(Notification $notification): string
{
return $this->phone_number;
}
}
Slack 通知
前提条件
发送 Slack 通知之前,应通过 Composer 安装 Slack 通知渠道:
composer require laravel/slack-notification-channel
此外,必须为 Slack 工作区创建一个 Slack App。
如果只需向创建 App 的同一 Slack 工作区发送通知,应确保 App 具有 chat:write、chat:write.public 和 chat:write.customize 权限范围。这些权限范围可以从 Slack 中的「OAuth & Permissions」App 管理标签页添加。
接下来,复制 App 的「Bot User OAuth Token」并将其放在应用 services.php 配置文件的 slack 配置数组中。此令牌可以在 Slack 中的「OAuth & Permissions」标签页上找到:
'slack' => [
'notifications' => [
'bot_user_oauth_token' => env('SLACK_BOT_USER_OAUTH_TOKEN'),
'channel' => env('SLACK_BOT_USER_DEFAULT_CHANNEL'),
],
],
App 分发
如果你的应用将向应用用户拥有的外部 Slack 工作区发送通知,需要通过 Slack「分发」你的 App。App 分发可以从 Slack 中的 App「Manage Distribution」标签页管理。App 分发后,可以使用 Socialite 代表应用用户获取 Slack Bot 令牌。
格式化 Slack 通知
如果通知支持作为 Slack 消息发送,应在通知类上定义 toSlack() 方法。此方法接收一个 $notifiable 实体,应返回一个 Illuminate\Notifications\Slack\SlackMessage 实例。可以使用 Slack 的 Block Kit API 构建丰富的通知:
use Illuminate\Notifications\Slack\BlockKit\Blocks\ContextBlock;
use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock;
use Illuminate\Notifications\Slack\SlackMessage;
/**
* 获取通知的 Slack 表示。
*/
public function toSlack(object $notifiable): SlackMessage
{
return (new SlackMessage)
->text('One of your invoices has been paid!')
->headerBlock('Invoice Paid')
->contextBlock(function (ContextBlock $block) {
$block->text('Customer #1234');
})
->sectionBlock(function (SectionBlock $block) {
$block->text('An invoice has been paid.');
$block->field("*Invoice No:*\n1000")->markdown();
$block->field("*Invoice Recipient:*\ntaylor@laravel.com")->markdown();
})
->dividerBlock()
->sectionBlock(function (SectionBlock $block) {
$block->text('Congratulations!');
});
}
使用 Slack Block Kit Builder 模板
除了使用流畅的消息构建器方法来构造 Block Kit 消息外,还可以将 Slack Block Kit Builder 生成的原始 JSON 负载提供给 usingBlockKitTemplate() 方法:
use Illuminate\Notifications\Slack\SlackMessage;
use Illuminate\Support\Str;
/**
* 获取通知的 Slack 表示。
*/
public function toSlack(object $notifiable): SlackMessage
{
$template = <<<JSON
{
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Team Announcement"
}
},
{
"type": "section",
"text": {
"type": "plain_text",
"text": "We are hiring!"
}
}
]
}
JSON;
return (new SlackMessage)
->usingBlockKitTemplate($template);
}
Slack 交互
Slack 的 Block Kit 通知系统提供了强大的功能来处理用户交互。要使用这些功能,你的 Slack App 应启用「Interactivity」并配置指向应用提供的 URL 的「Request URL」。这些设置可以从 Slack 中的「Interactivity & Shortcuts」App 管理标签页管理。
在以下使用 actionsBlock() 方法的示例中,Slack 将向你的「Request URL」发送 POST 请求,包含点击按钮的 Slack 用户、点击按钮的 ID 等信息的负载。你的应用可以根据负载确定要采取的操作。你还应验证请求确实来自 Slack:
use Illuminate\Notifications\Slack\BlockKit\Blocks\ActionsBlock;
use Illuminate\Notifications\Slack\BlockKit\Blocks\ContextBlock;
use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock;
use Illuminate\Notifications\Slack\SlackMessage;
/**
* 获取通知的 Slack 表示。
*/
public function toSlack(object $notifiable): SlackMessage
{
return (new SlackMessage)
->text('One of your invoices has been paid!')
->headerBlock('Invoice Paid')
->contextBlock(function (ContextBlock $block) {
$block->text('Customer #1234');
})
->sectionBlock(function (SectionBlock $block) {
$block->text('An invoice has been paid.');
})
->actionsBlock(function (ActionsBlock $block) {
// ID 默认为 "button_acknowledge_invoice"...
$block->button('Acknowledge Invoice')->primary();
// 手动配置 ID...
$block->button('Deny')->danger()->id('deny_invoice');
});
}
确认对话框
如果希望用户在执行操作前必须确认,可以在定义按钮时调用 confirm() 方法。confirm() 方法接受一个消息和一个接收 ConfirmObject 实例的闭包:
use Illuminate\Notifications\Slack\BlockKit\Blocks\ActionsBlock;
use Illuminate\Notifications\Slack\BlockKit\Blocks\ContextBlock;
use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock;
use Illuminate\Notifications\Slack\BlockKit\Composites\ConfirmObject;
use Illuminate\Notifications\Slack\SlackMessage;
/**
* 获取通知的 Slack 表示。
*/
public function toSlack(object $notifiable): SlackMessage
{
return (new SlackMessage)
->text('One of your invoices has been paid!')
->headerBlock('Invoice Paid')
->contextBlock(function (ContextBlock $block) {
$block->text('Customer #1234');
})
->sectionBlock(function (SectionBlock $block) {
$block->text('An invoice has been paid.');
})
->actionsBlock(function (ActionsBlock $block) {
$block->button('Acknowledge Invoice')
->primary()
->confirm(
'Acknowledge the payment and send a thank you email?',
function (ConfirmObject $dialog) {
$dialog->confirm('Yes');
$dialog->deny('No');
}
);
});
}
检查 Slack Blocks
如果想快速检查你构建的 blocks,可以在 SlackMessage 实例上调用 dd() 方法。dd() 方法将生成并转储一个指向 Slack Block Kit Builder 的 URL,在浏览器中显示负载和通知的预览。可以向 dd() 方法传递 true 来转储原始负载:
return (new SlackMessage)
->text('One of your invoices has been paid!')
->headerBlock('Invoice Paid')
->dd();
路由 Slack 通知
要将 Slack 通知定向到适当的 Slack 团队和频道,在可通知模型上定义 routeNotificationForSlack() 方法。此方法可以返回三种值之一:
null— 将路由延迟到通知本身配置的频道。可以在构建SlackMessage时使用to()方法在通知内配置频道。- 指定 Slack 频道的字符串,例如
#support-channel。 SlackRoute实例,允许你指定 OAuth 令牌和频道名,例如SlackRoute::make($this->slack_channel, $this->slack_token)。此方法应用于向外部工作区发送通知。
例如,从 routeNotificationForSlack() 方法返回 #support-channel 会将通知发送到应用 services.php 配置文件中 Bot User OAuth 令牌关联的工作区中的 #support-channel 频道:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
class User extends Authenticatable
{
use Notifiable;
/**
* 为 Slack 渠道路由通知。
*/
public function routeNotificationForSlack(Notification $notification): mixed
{
return '#support-channel';
}
}
通知外部 Slack 工作区
信息
在向外部 Slack 工作区发送通知之前,你的 Slack App 必须已分发。
当然,你通常会想向应用用户拥有的 Slack 工作区发送通知。为此,首先需要获取用户的 Slack OAuth 令牌。好在 Laravel Socialite 包含一个 Slack 驱动,可以让你轻松地用 Slack 认证应用用户并获取 bot 令牌。
获取 bot 令牌并将其存储在应用的数据库中后,可以利用 SlackRoute::make 方法将通知路由到用户的工作区。此外,你的应用可能还需要提供让用户指定通知应发送到哪个频道的机会:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Slack\SlackRoute;
class User extends Authenticatable
{
use Notifiable;
/**
* 为 Slack 渠道路由通知。
*/
public function routeNotificationForSlack(Notification $notification): mixed
{
return SlackRoute::make($this->slack_channel, $this->slack_token);
}
}
本地化通知
Laravel 允许你以 HTTP 请求当前语言环境以外的语言环境发送通知,如果通知被队列发送,还会记住此语言环境。
为此,Illuminate\Notifications\Notification 类提供了 locale() 方法来设置所需语言。当通知被评估时,应用将切换到此语言环境,评估完成后恢复到之前的语言环境:
$user->notify((new InvoicePaid($invoice))->locale('es'));
也可以通过 Notification 门面实现多个可通知条目的本地化:
Notification::locale('es')->send(
$users, new InvoicePaid($invoice)
);
用户首选语言环境
有时,应用会存储每个用户的首选语言环境。通过在可通知模型上实现 HasLocalePreference 契约,可以指示 Laravel 在发送通知时使用此存储的语言环境:
use Illuminate\Contracts\Translation\HasLocalePreference;
class User extends Model implements HasLocalePreference
{
/**
* 获取用户首选语言环境。
*/
public function preferredLocale(): string
{
return $this->locale;
}
}
实现接口后,Laravel 在向模型发送通知和邮件时会自动使用首选语言环境。因此,使用此接口时无需调用 locale() 方法:
$user->notify(new InvoicePaid($invoice));
测试
可以使用 Notification 门面的 fake() 方法阻止通知被发送。通常,发送通知与你实际测试的代码无关。很可能只需断言 Laravel 已被指示发送给定通知即可。
调用 Notification 门面的 fake() 方法后,可以断言通知已被指示发送给用户,甚至检查通知接收的数据:
<?php
use App\Notifications\OrderShipped;
use Illuminate\Support\Facades\Notification;
test('orders can be shipped', function () {
Notification::fake();
// 执行订单发货...
// 断言没有发送任何通知...
Notification::assertNothingSent();
// 断言已向给定用户发送通知...
Notification::assertSentTo(
[$user], OrderShipped::class
);
// 断言未发送某通知...
Notification::assertNotSentTo(
[$user], AnotherNotification::class
);
// 断言通知发送了两次...
Notification::assertSentTimes(WeeklyReminder::class, 2);
// 断言发送了给定数量的通知...
Notification::assertCount(3);
});
<?php
namespace Tests\Feature;
use App\Notifications\OrderShipped;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function test_orders_can_be_shipped(): void
{
Notification::fake();
// 执行订单发货...
// 断言没有发送任何通知...
Notification::assertNothingSent();
// 断言已向给定用户发送通知...
Notification::assertSentTo(
[$user], OrderShipped::class
);
// 断言未发送某通知...
Notification::assertNotSentTo(
[$user], AnotherNotification::class
);
// 断言通知发送了两次...
Notification::assertSentTimes(WeeklyReminder::class, 2);
// 断言发送了给定数量的通知...
Notification::assertCount(3);
}
}
可以向 assertSentTo 或 assertNotSentTo 方法传递闭包,以断言已发送通过给定「真值测试」的通知。如果至少发送了一封通过给定真值测试的通知,则断言成功:
Notification::assertSentTo(
$user,
function (OrderShipped $notification, array $channels) use ($order) {
return $notification->order->id === $order->id;
}
);
按需通知
如果你测试的代码发送的是按需通知,可以通过 assertSentOnDemand 方法测试按需通知是否已发送:
Notification::assertSentOnDemand(OrderShipped::class);
通过将闭包作为第二个参数传递给 assertSentOnDemand 方法,可以确定按需通知是否发送到了正确的「路由」地址:
Notification::assertSentOnDemand(
OrderShipped::class,
function (OrderShipped $notification, array $channels, object $notifiable) use ($user) {
return $notifiable->routes['mail'] === $user->email;
}
);
通知事件
通知发送事件
当通知正在发送时,通知系统会分派 Illuminate\Notifications\Events\NotificationSending 事件。它包含「可通知」实体和通知实例本身。你可以在应用中为此事件创建事件监听器:
use Illuminate\Notifications\Events\NotificationSending;
class CheckNotificationStatus
{
/**
* 处理事件。
*/
public function handle(NotificationSending $event): void
{
// ...
}
}
如果 NotificationSending 事件的事件监听器从其 handle() 方法返回 false,通知将不会被发送:
/**
* 处理事件。
*/
public function handle(NotificationSending $event): bool
{
return false;
}
在事件监听器中,可以访问事件上的 notifiable、notification 和 channel 属性,以了解更多关于通知收件人或通知本身的信息:
/**
* 处理事件。
*/
public function handle(NotificationSending $event): void
{
// $event->channel
// $event->notifiable
// $event->notification
}
通知已发送事件
当通知已发送时,通知系统会分派 Illuminate\Notifications\Events\NotificationSent 事件。它包含「可通知」实体和通知实例本身。你可以在应用中为此事件创建事件监听器:
use Illuminate\Notifications\Events\NotificationSent;
class LogNotification
{
/**
* 处理事件。
*/
public function handle(NotificationSent $event): void
{
// ...
}
}
在事件监听器中,可以访问事件上的 notifiable、notification、channel 和 response 属性,以了解更多关于通知收件人或通知本身的信息:
/**
* 处理事件。
*/
public function handle(NotificationSent $event): void
{
// $event->channel
// $event->notifiable
// $event->notification
// $event->response
}
自定义渠道
Laravel 附带了一些通知渠道,但你可能想编写自己的驱动来通过其他渠道投递通知。Laravel 使这变得简单。首先,定义一个包含 send() 方法的类。该方法应接收两个参数:$notifiable 和 $notification。
在 send() 方法中,可以调用通知上的方法来检索你的渠道能理解的消息对象,然后按你希望的方式将通知发送到 $notifiable 实例:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notification;
class VoiceChannel
{
/**
* 发送给定通知。
*/
public function send(object $notifiable, Notification $notification): void
{
$message = $notification->toVoice($notifiable);
// 将通知发送到 $notifiable 实例...
}
}
定义通知渠道类后,可以在任何通知的 via() 方法中返回该类名。在此示例中,通知的 toVoice() 方法可以返回你选择的任何表示语音消息的对象。例如,你可以定义自己的 VoiceMessage 类来表示这些消息:
<?php
namespace App\Notifications;
use App\Notifications\Messages\VoiceMessage;
use App\Notifications\VoiceChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
class InvoicePaid extends Notification
{
use Queueable;
/**
* 获取通知渠道。
*/
public function via(object $notifiable): string
{
return VoiceChannel::class;
}
/**
* 获取通知的语音表示。
*/
public function toVoice(object $notifiable): VoiceMessage
{
// ...
}
}