Deliver notifications in email

Hello,

I am wondering if there is any way to extend the Filament Notification class to be able to send notifications in email in a way that is similar to
->toBroadcast($user)
that is like
->toEmail($user)
. It doesn't need to be the most perfect email but my code is mostly based around Filament notifications so something simple to implement across the whole app is required.
Solution
<?php

namespace App\Notifications;

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

class FilamentNotificationEmail extends Notification
{
    use Queueable;

    protected \Filament\Notifications\Notification $filament_notification;

    /**
     * Create a new notification instance.
     */
    public function __construct(\Filament\Notifications\Notification $filament_notification)
    {
        $this->filament_notification = $filament_notification;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     */
    public function toMail(object $notifiable): MailMessage
    {

        $action = $this->filament_notification->getActions();
        $action = $action[0] ?? null;

        return (new MailMessage)
                    ->subject($this->filament_notification->getTitle())
                    ->greeting('Hello ' . $notifiable->name . ',')
                    ->line($this->filament_notification->getBody())
                    ->action($action ? $action->getLabel() : "Visit App", $action ? $action->getUrl() : env("APP_URL"));
    }

    /**
     * Get the array representation of the notification.
     *
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            //
        ];
    }
}
Was this page helpful?