Laravel 11 Xây dựng channels riêng cho notification

Laravel 11 Xây dựng channels riêng cho notification

Laravel Notification là một tin nhắn gửi đến người dùng app .Thông thường thì laravel có đầy đủ các kênh để gửi đến người dùng nhưng cũng có 1 số bạn muốn custom để có thể gửi đến các kênh đặc biệt của chính mình .Ở đây mình sử dụng kênh đặc biệt đó là ZaloChannel , kênh mà laravel chưa có 

Channel :

<?php

namespace App\Notifications\Channels;

class ZaloChannel
{
    public function send($notifiable, $notification)
    {
        $data = $notification->toArray($notifiable);

        // Send notification to the $notifiable instance...
    }
}

Ở đây mình lấy data là toArray , bạn cũng có thể custom dữ liệu là toZalo 

Ở file Notification : 

<?php

namespace App\Notifications;

use App\Notifications\Channels\ZaloChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class DathangNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     */
    public $data;
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        // return ['mail'];
        // return ['database'];
        // return ['zalo'];
        return [ZaloChannel::class, 'database'];
    }
    public function toZalo($notifiable): array
    {
        return [
            'message' => 'This is a Zalo message!',
            'data' => $this->data,
        ];
    }
    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
            ->line('The introduction to the notification.')
            ->action('Notification Action', url('/'))
            ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            'data' => $this->data,
        ];
    }
}

Ở đây mình sử dụng function toZalo để truyền dữ liệu sang bên Channel .Bạn cũng có custom tên channel để dễ sử dụng ở các Notification khác bằng cách thêm ở phương thức provider 

<?php

namespace App\Providers;

use App\Notifications\Channels\Zalo;
use App\Notifications\Channels\ZaloChannel;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void {}

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Notification::extend('zalo', function ($app) {
            return new ZaloChannel();
        });
    }
}

Thế là xong