Laravel 5.3 引入了全新的 Notification 通知系统,该系统默认支持 mail,database 和 array 三种 channel,但是在实际的开发中,这些默认的 channel 有可能满足实际的需求,比如在国内使用邮件发送的时候,该如何使用 Sendcloud 来集成,使得 Notification 也可以正常使用起来。这个时候可以使用下面的方法来自定义 Channel:
1.在 app/ 目录下创建 Channels/ 目录,并在该目录内创建自定义的 Channel 类,比如 SendCloudChannel:
namespace App\Channels;
use Illuminate\Notifications\Notification;
/**
* Class SendCloudChannel
* @package App\Channels
*/
class SendCloudChannel
{
/**
* @param $notifiable
* @param Notification $notification
*/
public function send($notifiable, Notification $notification)
{
$message = $notification->toSendcloud($notifiable); // 注意这个 toSendcloud 的方法,在使用的时候要一致
}
}注意在这个类里面,一定要实现 send() 这个方法。然后在有了这个 SendCloudChannel 类之后,在使用的使用可以直接以 SendCloudChannel::class 的方式来使用,比如 在 NewUserFollowNotification 中可以这样使用:
class NewUserFollowNotification extends Notification
{
public function via($notifiable)
{
return [SendCloudChannel::class,'database']; // 注意这里面的 sendCloudChannel::class 就是我们自定义的
}
public function toSendcloud($notifiable)
{
// 发送邮件的具体代码.
}
}注意 toSendcloud($notifiable) 自定义 SendCloudChannel 中 send() 方法体的 $notification->toSendcloud($notifiable) 的两个 toSendcloud 名字要一致。