Skip to main content
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.
return
Notification
Returns a new Notification instance
$notification = Notification::new();

Configuration Methods

title

Set the notification title.
title
string
required
The title text for the notification
return
self
Returns the Notification instance for method chaining
$notification->title('New Message');

message

Set the notification body text.
body
string
required
The body text for the notification
return
self
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.
reference
string
required
A unique identifier for this notification
return
self
Returns the Notification instance for method chaining
$notification->reference('msg-' . $message->id);

event

Set a Laravel event to dispatch when the notification is clicked.
event
string
required
The event identifier or class name
return
self
Returns the Notification instance for method chaining
$notification->event('notification.clicked');

sound

Set a custom sound for the notification.
sound
string
required
The name of the sound to play
return
self
Returns the Notification instance for method chaining
$notification->sound('default');

silent

Make the notification silent (no sound).
silent
bool
default:"true"
Whether the notification should be silent
return
self
Returns the Notification instance for method chaining
$notification->silent();
// or
$notification->silent(false);

hasReply

Enable a reply field in the notification.
placeholder
string
default:"''"
Placeholder text for the reply field
return
self
Returns the Notification instance for method chaining
$notification->hasReply('Type your reply...');

addAction

Add an action button to the notification.
label
string
required
The text label for the action button
return
self
Returns the Notification instance for method chaining
$notification
    ->addAction('View')
    ->addAction('Dismiss');

show

Display the notification.
return
self
Returns the Notification instance with the reference property populated
$notification->show();

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
    }
}

Build docs developers (and LLMs) love