From f9febd9444761dc8e3a3f32f85731a14b99bfafd Mon Sep 17 00:00:00 2001 From: Andreas Creten Date: Thu, 20 Feb 2025 09:06:45 +0100 Subject: [PATCH 1/7] Add support for notification reference --- src/Notification.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Notification.php b/src/Notification.php index 8bde4d59..2a52c3dd 100644 --- a/src/Notification.php +++ b/src/Notification.php @@ -6,6 +6,8 @@ class Notification { + protected ?string $reference = null; + protected string $title; protected string $body; @@ -22,6 +24,13 @@ public static function new() return new static(new Client); } + public function reference(string $reference): self + { + $this->reference = $reference; + + return $this; + } + public function title(string $title): self { $this->title = $title; @@ -43,12 +52,17 @@ public function message(string $body): self return $this; } - public function show(): void + public function show(): self { - $this->client->post('notification', [ + $response = $this->client->post('notification', [ + 'reference' => $this->reference, 'title' => $this->title, 'body' => $this->body, 'event' => $this->event, ]); + + $this->reference = $response->json('reference'); + + return $this; } } From d1ce38806caa8a76a310fdf6b2b6720f3691fc93 Mon Sep 17 00:00:00 2001 From: Andreas Creten Date: Thu, 20 Feb 2025 09:07:11 +0100 Subject: [PATCH 2/7] Add reference to Notification clicked class --- src/Events/Notifications/NotificationClicked.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Events/Notifications/NotificationClicked.php b/src/Events/Notifications/NotificationClicked.php index ea01ace1..c3cc4f5f 100644 --- a/src/Events/Notifications/NotificationClicked.php +++ b/src/Events/Notifications/NotificationClicked.php @@ -12,6 +12,8 @@ class NotificationClicked implements ShouldBroadcastNow { use Dispatchable, InteractsWithSockets, SerializesModels; + public function __construct(public string $reference, public string $event) {} + public function broadcastOn() { return [ From 2281f1cd6c3e8773b5e38290b4fa2fb3b3fa7d5b Mon Sep 17 00:00:00 2001 From: Andreas Creten Date: Thu, 20 Feb 2025 09:07:22 +0100 Subject: [PATCH 3/7] Support for more notification actions --- .../NotificationActionClicked.php | 23 +++++++++++++++++++ .../Notifications/NotificationClosed.php | 23 +++++++++++++++++++ .../Notifications/NotificationReply.php | 23 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 src/Events/Notifications/NotificationActionClicked.php create mode 100644 src/Events/Notifications/NotificationClosed.php create mode 100644 src/Events/Notifications/NotificationReply.php diff --git a/src/Events/Notifications/NotificationActionClicked.php b/src/Events/Notifications/NotificationActionClicked.php new file mode 100644 index 00000000..de658b3c --- /dev/null +++ b/src/Events/Notifications/NotificationActionClicked.php @@ -0,0 +1,23 @@ + Date: Thu, 20 Feb 2025 09:09:15 +0100 Subject: [PATCH 4/7] Make reference public --- src/Facades/Notification.php | 1 + src/Notification.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Facades/Notification.php b/src/Facades/Notification.php index 425fea13..f95df64e 100644 --- a/src/Facades/Notification.php +++ b/src/Facades/Notification.php @@ -8,6 +8,7 @@ * @method static static title(string $title) * @method static static event(string $event) * @method static static message(string $body) + * @method static static reference(string $reference) * @method static void show() */ class Notification extends Facade diff --git a/src/Notification.php b/src/Notification.php index 2a52c3dd..ac3d767a 100644 --- a/src/Notification.php +++ b/src/Notification.php @@ -6,7 +6,7 @@ class Notification { - protected ?string $reference = null; + public ?string $reference = null; protected string $title; From 10837d5ec995f8cff00cd6450804205ebd0774e8 Mon Sep 17 00:00:00 2001 From: Andreas Creten Date: Thu, 20 Feb 2025 21:11:16 +0100 Subject: [PATCH 5/7] Support for actions and reply --- src/Notification.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Notification.php b/src/Notification.php index ac3d767a..d6dd812d 100644 --- a/src/Notification.php +++ b/src/Notification.php @@ -14,6 +14,12 @@ class Notification protected string $event = ''; + private bool $hasReply = false; + + private string $replyPlaceholder = ''; + + private array $actions = []; + final public function __construct(protected Client $client) { $this->title = config('app.name'); @@ -45,6 +51,21 @@ public function event(string $event): self return $this; } + public function hasReply(string $placeholder = ''): self + { + $this->hasReply = true; + $this->replyPlaceholder = $placeholder; + + return $this; + } + + public function addAction(string $label): self + { + $this->actions[] = $label; + + return $this; + } + public function message(string $body): self { $this->body = $body; @@ -59,6 +80,9 @@ public function show(): self 'title' => $this->title, 'body' => $this->body, 'event' => $this->event, + 'hasReply' => $this->hasReply, + 'replyPlaceholder' => $this->replyPlaceholder, + 'actions' => $this->actions, ]); $this->reference = $response->json('reference'); From 53783404f0b8615d98cf08741975b11c1c7c6812 Mon Sep 17 00:00:00 2001 From: Andreas Creten Date: Thu, 20 Feb 2025 21:14:36 +0100 Subject: [PATCH 6/7] Add missing methods to the facade --- src/Facades/Notification.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Facades/Notification.php b/src/Facades/Notification.php index f95df64e..5cb78169 100644 --- a/src/Facades/Notification.php +++ b/src/Facades/Notification.php @@ -9,6 +9,8 @@ * @method static static event(string $event) * @method static static message(string $body) * @method static static reference(string $reference) + * @method static static hasReply(string $placeholder = '') + * @method static static addAction(string $label) * @method static void show() */ class Notification extends Facade From faf9b439f22507332d286f71ef5e51870199b574 Mon Sep 17 00:00:00 2001 From: Andreas Creten Date: Thu, 20 Feb 2025 21:34:53 +0100 Subject: [PATCH 7/7] Properly send the actions --- src/Notification.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Notification.php b/src/Notification.php index d6dd812d..a1da4847 100644 --- a/src/Notification.php +++ b/src/Notification.php @@ -82,7 +82,10 @@ public function show(): self 'event' => $this->event, 'hasReply' => $this->hasReply, 'replyPlaceholder' => $this->replyPlaceholder, - 'actions' => $this->actions, + 'actions' => array_map(fn(string $label) => [ + 'type' => 'button', + 'text' => $label + ], $this->actions), ]); $this->reference = $response->json('reference');