Basics
By default, MoonShine uses Laravel Database Notification, but we use abstractions that can be easily replaced.
If you need to add notifications to the MoonShine notification center, use the MoonShine\Laravel\Notifications\MoonShineNotification
class.
Directly via the static method send()
:
use MoonShine\Laravel\Notifications\MoonShineNotification;use MoonShine\Laravel\Notifications\NotificationButton;use MoonShine\Support\Enums\Color;MoonShineNotification::send(message: 'Notification text',// Optional buttonbutton: new NotificationButton('Click me', 'https://moonshine.cutcode.dev', attributes: ['target' => '_blank']),// Optional administrator IDs (default for all)ids: [1,2,3],// Optional icon colorcolor: Color::GREEN,// Optional iconicon: 'information-circle');
use MoonShine\Laravel\Notifications\MoonShineNotification;use MoonShine\Laravel\Notifications\NotificationButton;use MoonShine\Support\Enums\Color;MoonShineNotification::send(message: 'Notification text',// Optional buttonbutton: new NotificationButton('Click me', 'https://moonshine.cutcode.dev', attributes: ['target' => '_blank']),// Optional administrator IDs (default for all)ids: [1,2,3],// Optional icon colorcolor: Color::GREEN,// Optional iconicon: 'information-circle');
Or via DI
:
use MoonShine\Laravel\Contracts\Notifications\MoonShineNotificationContract;public function di(MoonShineNotificationContract $notification){$notification->notify('Hello');}
use MoonShine\Laravel\Contracts\Notifications\MoonShineNotificationContract;public function di(MoonShineNotificationContract $notification){$notification->notify('Hello');}
Settings
During the installation of MoonShine, you have the option to choose whether you want to use notifications and Database Notification
.
Additionally, you can change these settings later through the configuration:
'use_notifications' => true,'use_database_notifications' => true,
'use_notifications' => true,'use_database_notifications' => true,
$config->useNotifications()->useDatabaseNotifications();
$config->useNotifications()->useDatabaseNotifications();
Component
The component MoonShine\Laravel\Components\Layout\Notifications
is used to display notifications, which you can replace with your own through Layout.
Custom Notifications
MoonShine is flexible and everything can be replaced with your own implementations; for notifications, you need to implement the interfaces:
MoonShine\Laravel\Contracts\Notifications\MoonShineNotificationContract
MoonShine\Laravel\Contracts\Notifications\NotificationItemContract
MoonShine\Laravel\Contracts\Notifications\NotificationButtonContract
(optional)
Then, in the ServiceProvider, replace the implementation with your own:
public function boot(): void{$this->app->singleton(MoonShineNotificationContract::class,MyNotificationSystem::class);}
public function boot(): void{$this->app->singleton(MoonShineNotificationContract::class,MyNotificationSystem::class);}
WebSocket
A ready-made implementation of notifications via WebSocket is available in the Rush package.