Overview
To start, make sure the package is installed - @livewire('notifications') should be in your Blade layout somewhere.
Notifications are sent using a Notification object that’s constructed through a fluent API. Calling the send() method on the Notification object will dispatch the notification and display it in your application. As the session is used to flash notifications, they can be sent from anywhere in your code, including JavaScript, not just Livewire components.
Setting a title
The main message of the notification is shown in the title. You can set the title as follows:Str::markdown() helper: title(Str::markdown('Saved **successfully**'))
Or with JavaScript:
Setting an icon
Optionally, a notification can have an icon that’s displayed in front of its content. You may also set a color for the icon, which is gray by default:success, warning, danger or info. Instead of manually setting the corresponding icons and colors, there’s a status() method which you can pass the status. You may also use the dedicated success(), warning(), danger() and info() methods instead. So, cleaning up the above example would look like this:
Setting a background color
Notifications have no background color by default. You may want to provide additional context to your notification by setting a color as follows:Setting a duration
By default, notifications are shown for 6 seconds before they’re automatically closed. You may specify a custom duration value in milliseconds as follows:Setting body text
Additional notification text can be shown in thebody():
Str::markdown() helper: body(Str::markdown('Changes to the **post** have been saved.'))
Or with JavaScript:
Adding actions to notifications
Notifications support Actions, which are buttons that render below the content of the notification. They can open a URL or dispatch a Livewire event. Actions can be defined as follows:Opening URLs from notification actions
You can open a URL, optionally in a new tab, when clicking on an action:Dispatching Livewire events from notification actions
Sometimes you want to execute additional code when a notification action is clicked. This can be achieved by setting a Livewire event which should be dispatched on clicking the action. You may optionally pass an array of data, which will be available as parameters in the event listener on your Livewire component:dispatchSelf and dispatchTo:
dispatchSelf and dispatchTo are also available:
Closing notifications from actions
After opening a URL or dispatching an event from your action, you may want to close the notification right away:Using the JavaScript objects
The JavaScript objects (FilamentNotification and FilamentNotificationAction) are assigned to window.FilamentNotification and window.FilamentNotificationAction, so they are available in on-page scripts.
You may also import them in a bundled JavaScript file:
Closing a notification with JavaScript
Once a notification has been sent, you can close it on demand by dispatching a browser event on the window calledclose-notification.
The event needs to contain the ID of the notification you sent. To get the ID, you can use the getId() method on the Notification object: