Laravel 8 自定義通道

2021-07-06 10:30 更新

Laravel 為我們提供了許多通知頻道,但是您亦可編寫自己驅(qū)動(dòng)以通過其他頻道來發(fā)送通知,這并不難。首先,請(qǐng)定義一個(gè)包含了 send 方法的類。該方法應(yīng)該接收兩個(gè)參數(shù): $notifiable$notification

<?php

namespace App\Channels;

use Illuminate\Notifications\Notification;

class VoiceChannel
{
    /**
     * 發(fā)送指定的通知。
     *
     * @param  mixed  $notifiable
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return void
     */
    public function send($notifiable, Notification $notification)
    {
        $message = $notification->toVoice($notifiable);

        // 發(fā)送通知到 $notifiable 實(shí)例中……
    }
} 

一旦您的通知頻道定義完成后,您便可以在應(yīng)用中通過 via 方法來返回類名:

<?php

namespace App\Notifications;

use App\Channels\Messages\VoiceMessage;
use App\Channels\VoiceChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;

class InvoicePaid extends Notification
{
    use Queueable;

    /**
     * 獲取通知頻道。
     *
     * @param  mixed  $notifiable
     * @return array|string
     */
    public function via($notifiable)
    {
        return [VoiceChannel::class];
    }

    /**
     * 獲取語音形式的通知。
     *
     * @param  mixed  $notifiable
     * @return VoiceMessage
     */
    public function toVoice($notifiable)
    {
        // ...
    }
} 
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)