The Notification API allows you to display native system notifications with support for custom titles, messages, actions, replies, and sounds.
Basic Usage
use Native\Desktop\Notification;
Notification::new()
->title('New Message')
->message('You have a new message from John')
->show();
Creating Notifications
new
Create a new notification instance.
Returns a new Notification instance
$notification = Notification::new();
Configuration Methods
title
Set the notification title.
The title text for the notification
Returns the Notification instance for method chaining
$notification->title('New Message');
message
Set the notification body text.
The body text for the notification
Returns the Notification instance for method chaining
$notification->message('You have a new message from John');
reference
Set a unique reference identifier for the notification.
A unique identifier for this notification
Returns the Notification instance for method chaining
$notification->reference('msg-' . $message->id);
event
Set a Laravel event to dispatch when the notification is clicked.
The event identifier or class name
Returns the Notification instance for method chaining
$notification->event('notification.clicked');
sound
Set a custom sound for the notification.
The name of the sound to play
Returns the Notification instance for method chaining
$notification->sound('default');
silent
Make the notification silent (no sound).
Whether the notification should be silent
Returns the Notification instance for method chaining
$notification->silent();
// or
$notification->silent(false);
hasReply
Enable a reply field in the notification.
Placeholder text for the reply field
Returns the Notification instance for method chaining
$notification->hasReply('Type your reply...');
addAction
Add an action button to the notification.
The text label for the action button
Returns the Notification instance for method chaining
$notification
->addAction('View')
->addAction('Dismiss');
show
Display the notification.
Returns the Notification instance with the reference property populated
Properties
reference
After calling show(), the reference property will be automatically populated if not set manually.
$notification = Notification::new()
->title('Test')
->message('Hello')
->show();
$id = $notification->reference;
Complete Examples
Simple Notification
use Native\Desktop\Notification;
Notification::new()
->title('Task Complete')
->message('Your export has finished successfully')
->show();
Notification with Actions
use Native\Desktop\Notification;
Notification::new()
->title('New Message')
->message('You have a new message from John')
->addAction('View')
->addAction('Mark as Read')
->event('message.received')
->show();
Notification with Reply
use Native\Desktop\Notification;
Notification::new()
->title('Quick Reply')
->message('John sent you a message')
->hasReply('Type your reply...')
->event('message.reply')
->show();
Silent Notification with Reference
use Native\Desktop\Notification;
$notification = Notification::new()
->reference('update-' . time())
->title('Update Available')
->message('A new version is ready to install')
->silent()
->addAction('Install')
->addAction('Later')
->show();
Custom Sound Notification
use Native\Desktop\Notification;
Notification::new()
->title('Alert')
->message('This notification has a custom sound')
->sound('Glass')
->event('alert.sound')
->show();
Handling Notification Events
When a user interacts with a notification, you can handle it using Laravel events:
use Native\Desktop\Events\Notifications\NotificationClicked;
class NotificationClickedListener
{
public function handle(NotificationClicked $event)
{
// Access notification data
$reference = $event->reference;
$action = $event->action;
$reply = $event->reply;
// Handle the interaction
}
}