如何使用laravel通知中的via参数调用自定义函数



devs,我使用的是laravel 8,我在里面创建了一个通知,我在下面的代码中提到了我自己对Twilio的功能。

问题:如何调用该函数。我在via((函数的返回参数中包含但它向我展示了";不支持驱动程序[twilio]&";。我什么都不登记在任何地方我试图更改仍显示错误的函数的名称"不支持驱动程序[lt;fun_name>]。


<?php
namespace AppNotifications;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;
use IlluminateNotificationsNotification;
class DepartmentNotification extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param  mixed  $notifiable
* @return array
*/
public function via($notifiable)
{
return ['toTwilio'];
}
/**
* Get the mail representation of the notification.
*
* @param  mixed  $notifiable
* @return IlluminateNotificationsMessagesMailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
public function toTwilio($notifiable)
{
echo "twilio hit";
}
/**
* Get the array representation of the notification.
*
* @param  mixed  $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

在此处阅读有关自定义通知渠道的官方文档:https://laravel.com/docs/8.x/notifications#custom-通道。

首先,您应该创建一个TwilioChannel类来调用toTwilio方法:


<?php
namespace AppChannels;
use IlluminateNotificationsNotification;
class TwiolioChannel
{
public function send($notifiable, Notification $notification)
{
$message = $notification->toTwilio($notifiable);
// Send notification to the $notifiable instance...
}
}

创建Channel类更改通知后,via方法如下:

public function via($notifiable)
{
return [TwilioChannel::class];
}

您可以在此处找到更多详细信息https://laravel.com/docs/8.x/notifications#sending-通知。

但基本上,您可以通过以下两种方式发送通知:

1(使用notifiable特征

use Notifiable;
$user->notify(new DepartmentNotification());

2(使用通知立面

use IlluminateSupportFacadesNotification;
Notification::send($users, new DepartmentNotification());

此外,看起来您正在尝试对通知进行排队。这意味着您可能还必须运行php artisan queue:work

最新更新