Skip to content

Commit 80e4a92

Browse files
Update Notifications docs (#83)
* Update Notifications docs * Correct docs for ID instead of timestamp * Fix typo * Add examples of notification handling * Simplified --------- Co-authored-by: Simon Hamp <[email protected]>
1 parent 2769cf2 commit 80e4a92

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

resources/views/docs/desktop/1/the-basics/notifications.md

+135
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ NativePHP allows you to send system notifications using an elegant PHP API. Thes
1010
When used sparingly, notifications can be a great way to inform the user about events that are occurring in your application and to bring their attention back to it, especially if further input from them is required.
1111

1212
Notifications are sent using the `Notification` facade.
13+
1314
```php
1415
use Native\Laravel\Facades\Notification;
1516
```
@@ -40,8 +41,142 @@ Notification::title('Hello from NativePHP')
4041
->show();
4142
```
4243

44+
### Notification References
45+
46+
To keep track of different notifications, you may use the notification's `$reference` property.
47+
48+
By default, a unique reference is generated for you, but you may manually set a reference by [chaining the `reference()`](#notification-reference) method when creating
49+
the notification.
50+
51+
## Configuring Notifications
52+
53+
### Notification Title
54+
55+
You may set the notification's title using the `title()` method.
56+
57+
```php
58+
Notification::title('Hello from NativePHP')
59+
->show();
60+
```
61+
62+
### Notification Reference
63+
64+
You can access the `$reference` property of a notification after it has been created:
65+
66+
```
67+
$notification = Notification::title('Hello from NativePHP')->show();
68+
69+
$notification->reference;
70+
```
71+
72+
You may chain the `reference()` method to set a custom reference when creating a notification:
73+
74+
```php
75+
Notification::title('Hello from NativePHP')
76+
->reference(Str::uuid())
77+
->show();
78+
```
79+
80+
The reference will be sent along with any event triggered by the notification and can be used to track which specific notification was clicked:
81+
82+
```php
83+
use App\Events\PostNotificationClicked;
84+
use App\Models\Post;
85+
86+
Post::recentlyCreated()
87+
->get()
88+
->each(function(Post $post) {
89+
Notification::title('New post: ' . $post->title)
90+
->reference($post->id)
91+
->event(PostNotificationClicked::class)
92+
->show();
93+
});
94+
95+
Event::listen(PostNotificationClicked::class, function (PostNotificationClicked $event) {
96+
$post = Post::findOrFail($event->reference);
97+
98+
Window::open()->url($post->url);
99+
});
100+
```
101+
102+
### Notification Message
103+
104+
You may set the notification's message using the `message()` method.
105+
106+
```php
107+
Notification::title('Hello from NativePHP')
108+
->message('This is a detail message coming from your Laravel app.')
109+
->show();
110+
```
111+
112+
### Notification Reply
113+
114+
On macOS, you can allow the user to reply to a notification using the `hasReply()` method.
115+
116+
```php
117+
Notification::title('Hello from NativePHP')
118+
->hasReply()
119+
->show();
120+
```
121+
122+
The `hasReply()` method accepts a placeholder reply message as an argument.
123+
124+
```php
125+
Notification::title('Hello from NativePHP')
126+
->hasReply('This is a placeholder')
127+
->show();
128+
```
129+
130+
### Notification Actions
131+
132+
On macOS, you can add action buttons to a notification using the `addAction()` method.
133+
134+
```php
135+
Notification::title('Hello from NativePHP')
136+
->addAction('Click here')
137+
->show();
138+
```
139+
140+
You can call the `addAction()` method multiple times if you need to add multiple buttons.
141+
142+
```php
143+
Notification::title('Hello from NativePHP')
144+
->addAction('Button One')
145+
->addAction('Button Two')
146+
->show();
147+
```
148+
149+
When an action button is clicked, it will trigger the [`NotificationActionClicked`](#codenotificationactionclickedcode) event.
150+
151+
This event contains an `$index` property, which refers to the index of the action button that was clicked. Action button indexes start at `0`:
152+
153+
```php
154+
use Native\Laravel\Events\Notifications\NotificationActionClicked;
155+
156+
Notification::title('Do you accept?')
157+
->addAction('Accept') // This action will be $index = 0
158+
->addAction('Decline') // This action will be $index = 1
159+
->show();
160+
161+
Event::listen(NotificationActionClicked::class, function (NotificationActionClicked $event) {
162+
if ($event->index === 0) {
163+
// 'Accept' clicked
164+
} elseif ($event->index === 1) {
165+
// 'Decline' clicked
166+
}
167+
});
168+
```
169+
43170
## Events
44171

45172
### `NotificationClicked`
46173
The `Native\Laravel\Events\Notifications\NotificationClicked` event is dispatched when a user clicks on a notification.
47174

175+
### `NotificationClosed`
176+
The `Native\Laravel\Events\Notifications\NotificationClosed` event is dispatched when a user closes a notification.
177+
178+
### `NotificationReply`
179+
The `Native\Laravel\Events\Notifications\NotificationReply` event is dispatched when a user replies to a notification.
180+
181+
### `NotificationActionClicked`
182+
The `Native\Laravel\Events\Notifications\NotificationActionClicked` event is dispatched when a user clicks an action button on a notification.

0 commit comments

Comments
 (0)