Skip to content

Commit b777f41

Browse files
committed
Dispatch additional events
1 parent 622b292 commit b777f41

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

src/FcmChannel.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Illuminate\Contracts\Events\Dispatcher;
66
use Illuminate\Notifications\Events\NotificationFailed;
7+
use Illuminate\Notifications\Events\NotificationSending;
8+
use Illuminate\Notifications\Events\NotificationSent;
79
use Illuminate\Notifications\Notification;
810
use Illuminate\Support\Arr;
911
use Illuminate\Support\Collection;
@@ -35,37 +37,40 @@ public function send(mixed $notifiable, Notification $notification): ?Collection
3537
{
3638
$tokens = Arr::wrap($notifiable->routeNotificationFor('fcm', $notification));
3739

38-
if (empty($tokens)) {
39-
return null;
40-
}
41-
42-
$fcmMessage = $notification->toFcm($notifiable);
43-
4440
return Collection::make($tokens)
4541
->chunk(self::TOKENS_PER_REQUEST)
46-
->map(fn ($tokens) => ($fcmMessage->client ?? $this->client)->sendMulticast($fcmMessage, $tokens->all()))
47-
->map(fn (MulticastSendReport $report) => $this->checkReportForFailures($notifiable, $notification, $report));
42+
->map(fn ($tokens) => $this->sendNotifications($notifiable, $notification, $tokens))
43+
->map(fn (MulticastSendReport $report) => $this->dispatchEvents($notifiable, $notification, $report));
4844
}
4945

5046
/**
51-
* Handle the report for the notification and dispatch any failed notifications.
47+
* Send the notification with the provided tokens.
5248
*/
53-
protected function checkReportForFailures(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport
49+
protected function sendNotifications(mixed $notifiable, Notification $notification, Collection $tokens): MulticastSendReport
5450
{
55-
Collection::make($report->getItems())
56-
->filter(fn (SendReport $report) => $report->isFailure())
57-
->each(fn (SendReport $report) => $this->dispatchFailedNotification($notifiable, $notification, $report));
51+
$fcmMessage = $notification->toFcm($notifiable);
5852

59-
return $report;
53+
$this->events->dispatch(
54+
new NotificationSending($notifiable, $notification, self::class)
55+
);
56+
57+
return ($fcmMessage->client ?? $this->client)->sendMulticast($fcmMessage, $tokens->all());
6058
}
6159

6260
/**
63-
* Dispatch failed event.
61+
* Handle the report for the notification and dispatch any failed notifications.
6462
*/
65-
protected function dispatchFailedNotification(mixed $notifiable, Notification $notification, SendReport $report): void
63+
protected function dispatchEvents(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport
6664
{
67-
$this->events->dispatch(new NotificationFailed($notifiable, $notification, self::class, [
68-
'report' => $report,
69-
]));
65+
Collection::make($report->getItems())
66+
->each(function (SendReport $report) use ($notifiable, $notification) {
67+
$event = $report->isSuccess()
68+
? new NotificationSent($notifiable, $notification, self::class, compact('report'))
69+
: new NotificationFailed($notifiable, $notification, self::class, compact('report'));
70+
71+
$this->events->dispatch($event);
72+
});
73+
74+
return $report;
7075
}
7176
}

tests/FcmChannelTest.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Exception;
66
use Illuminate\Contracts\Events\Dispatcher;
77
use Illuminate\Notifications\Notifiable;
8+
use Illuminate\Notifications\Events\NotificationFailed;
9+
use Illuminate\Notifications\Events\NotificationSending;
10+
use Illuminate\Notifications\Events\NotificationSent;
811
use Illuminate\Notifications\Notification;
912
use Illuminate\Support\Collection;
1013
use Kreait\Firebase\Contract\Messaging;
@@ -36,7 +39,14 @@ public function test_it_can_be_instantiated()
3639
public function test_it_can_send_notifications()
3740
{
3841
$events = Mockery::mock(Dispatcher::class);
39-
$events->shouldNotReceive('dispatch');
42+
$events->shouldReceive('dispatch')
43+
->once()
44+
->with(Mockery::type(NotificationSending::class));
45+
$events->shouldReceive('dispatch')
46+
->once()
47+
->with(Mockery::type(NotificationSent::class));
48+
$events->shouldNotReceive('dispatch')
49+
->with(Mockery::type(NotificationFailed::class));
4050

4151
$firebase = Mockery::mock(Messaging::class);
4252
$firebase->shouldReceive('sendMulticast')
@@ -56,7 +66,14 @@ public function test_it_can_send_notifications()
5666
public function test_it_can_send_notifications_with_custom_client()
5767
{
5868
$events = Mockery::mock(Dispatcher::class);
59-
$events->shouldNotReceive('dispatch');
69+
$events->shouldReceive('dispatch')
70+
->once()
71+
->with(Mockery::type(NotificationSending::class));
72+
$events->shouldReceive('dispatch')
73+
->once()
74+
->with(Mockery::type(NotificationSent::class));
75+
$events->shouldNotReceive('dispatch')
76+
->with(Mockery::type(NotificationFailed::class));
6077

6178
$firebase = Mockery::mock(Messaging::class);
6279
$events->shouldNotReceive('sendMulticast');
@@ -78,7 +95,14 @@ public function test_it_can_send_notifications_with_custom_client()
7895
public function test_it_can_dispatch_events()
7996
{
8097
$events = Mockery::mock(Dispatcher::class);
81-
$events->shouldReceive('dispatch')->once();
98+
$events->shouldReceive('dispatch')
99+
->once()
100+
->with(Mockery::type(NotificationSending::class));
101+
$events->shouldNotReceive('dispatch')
102+
->with(Mockery::type(NotificationSent::class));
103+
$events->shouldReceive('dispatch')
104+
->once()
105+
->with(Mockery::type(NotificationFailed::class));
82106

83107
$firebase = Mockery::mock(Messaging::class);
84108
$firebase->shouldReceive('sendMulticast')

0 commit comments

Comments
 (0)