Skip to content

Commit dfcd9e2

Browse files
Notification improvements (#498)
* Add support for notification reference * Add reference to Notification clicked class * Support for more notification actions * Make reference public * Support for actions and reply * Add missing methods to the facade * Properly send the actions
1 parent 182c5d8 commit dfcd9e2

File tree

6 files changed

+117
-2
lines changed

6 files changed

+117
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\Notifications;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class NotificationActionClicked implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct(public string $reference, public int $index, public string $event) {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}

src/Events/Notifications/NotificationClicked.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class NotificationClicked implements ShouldBroadcastNow
1212
{
1313
use Dispatchable, InteractsWithSockets, SerializesModels;
1414

15+
public function __construct(public string $reference, public string $event) {}
16+
1517
public function broadcastOn()
1618
{
1719
return [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\Notifications;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class NotificationClosed implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct(public string $reference, public string $event) {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Native\Laravel\Events\Notifications;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class NotificationReply implements ShouldBroadcastNow
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public function __construct(public string $reference, public string $reply, public string $event) {}
16+
17+
public function broadcastOn()
18+
{
19+
return [
20+
new Channel('nativephp'),
21+
];
22+
}
23+
}

src/Facades/Notification.php

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
* @method static static title(string $title)
99
* @method static static event(string $event)
1010
* @method static static message(string $body)
11+
* @method static static reference(string $reference)
12+
* @method static static hasReply(string $placeholder = '')
13+
* @method static static addAction(string $label)
1114
* @method static void show()
1215
*/
1316
class Notification extends Facade

src/Notification.php

+43-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@
66

77
class Notification
88
{
9+
public ?string $reference = null;
10+
911
protected string $title;
1012

1113
protected string $body;
1214

1315
protected string $event = '';
1416

17+
private bool $hasReply = false;
18+
19+
private string $replyPlaceholder = '';
20+
21+
private array $actions = [];
22+
1523
final public function __construct(protected Client $client)
1624
{
1725
$this->title = config('app.name');
@@ -22,6 +30,13 @@ public static function new()
2230
return new static(new Client);
2331
}
2432

33+
public function reference(string $reference): self
34+
{
35+
$this->reference = $reference;
36+
37+
return $this;
38+
}
39+
2540
public function title(string $title): self
2641
{
2742
$this->title = $title;
@@ -36,19 +51,45 @@ public function event(string $event): self
3651
return $this;
3752
}
3853

54+
public function hasReply(string $placeholder = ''): self
55+
{
56+
$this->hasReply = true;
57+
$this->replyPlaceholder = $placeholder;
58+
59+
return $this;
60+
}
61+
62+
public function addAction(string $label): self
63+
{
64+
$this->actions[] = $label;
65+
66+
return $this;
67+
}
68+
3969
public function message(string $body): self
4070
{
4171
$this->body = $body;
4272

4373
return $this;
4474
}
4575

46-
public function show(): void
76+
public function show(): self
4777
{
48-
$this->client->post('notification', [
78+
$response = $this->client->post('notification', [
79+
'reference' => $this->reference,
4980
'title' => $this->title,
5081
'body' => $this->body,
5182
'event' => $this->event,
83+
'hasReply' => $this->hasReply,
84+
'replyPlaceholder' => $this->replyPlaceholder,
85+
'actions' => array_map(fn(string $label) => [
86+
'type' => 'button',
87+
'text' => $label
88+
], $this->actions),
5289
]);
90+
91+
$this->reference = $response->json('reference');
92+
93+
return $this;
5394
}
5495
}

0 commit comments

Comments
 (0)