Skip to content

Notification improvements #498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Events/Notifications/NotificationActionClicked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Native\Laravel\Events\Notifications;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class NotificationActionClicked implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct(public string $reference, public int $index, public string $event) {}

public function broadcastOn()
{
return [
new Channel('nativephp'),
];
}
}
2 changes: 2 additions & 0 deletions src/Events/Notifications/NotificationClicked.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
23 changes: 23 additions & 0 deletions src/Events/Notifications/NotificationClosed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Native\Laravel\Events\Notifications;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class NotificationClosed implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct(public string $reference, public string $event) {}

public function broadcastOn()
{
return [
new Channel('nativephp'),
];
}
}
23 changes: 23 additions & 0 deletions src/Events/Notifications/NotificationReply.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Native\Laravel\Events\Notifications;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class NotificationReply implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct(public string $reference, public string $reply, public string $event) {}

public function broadcastOn()
{
return [
new Channel('nativephp'),
];
}
}
3 changes: 3 additions & 0 deletions src/Facades/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* @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 static hasReply(string $placeholder = '')
* @method static static addAction(string $label)
* @method static void show()
*/
class Notification extends Facade
Expand Down
45 changes: 43 additions & 2 deletions src/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@

class Notification
{
public ?string $reference = null;

protected string $title;

protected string $body;

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');
Expand All @@ -22,6 +30,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;
Expand All @@ -36,19 +51,45 @@ 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;

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,
'hasReply' => $this->hasReply,
'replyPlaceholder' => $this->replyPlaceholder,
'actions' => array_map(fn(string $label) => [
'type' => 'button',
'text' => $label
], $this->actions),
]);

$this->reference = $response->json('reference');

return $this;
}
}