|
4 | 4 |
|
5 | 5 | use Illuminate\Contracts\Events\Dispatcher;
|
6 | 6 | use Illuminate\Notifications\Events\NotificationFailed;
|
| 7 | +use Illuminate\Notifications\Events\NotificationSending; |
| 8 | +use Illuminate\Notifications\Events\NotificationSent; |
7 | 9 | use Illuminate\Notifications\Notification;
|
8 | 10 | use Illuminate\Support\Arr;
|
9 | 11 | use Illuminate\Support\Collection;
|
@@ -35,37 +37,40 @@ public function send(mixed $notifiable, Notification $notification): ?Collection
|
35 | 37 | {
|
36 | 38 | $tokens = Arr::wrap($notifiable->routeNotificationFor('fcm', $notification));
|
37 | 39 |
|
38 |
| - if (empty($tokens)) { |
39 |
| - return null; |
40 |
| - } |
41 |
| - |
42 |
| - $fcmMessage = $notification->toFcm($notifiable); |
43 |
| - |
44 | 40 | return Collection::make($tokens)
|
45 | 41 | ->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)); |
48 | 44 | }
|
49 | 45 |
|
50 | 46 | /**
|
51 |
| - * Handle the report for the notification and dispatch any failed notifications. |
| 47 | + * Send the notification with the provided tokens. |
52 | 48 | */
|
53 |
| - protected function checkReportForFailures(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport |
| 49 | + protected function sendNotifications(mixed $notifiable, Notification $notification, Collection $tokens): MulticastSendReport |
54 | 50 | {
|
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); |
58 | 52 |
|
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()); |
60 | 58 | }
|
61 | 59 |
|
62 | 60 | /**
|
63 |
| - * Dispatch failed event. |
| 61 | + * Handle the report for the notification and dispatch any failed notifications. |
64 | 62 | */
|
65 |
| - protected function dispatchFailedNotification(mixed $notifiable, Notification $notification, SendReport $report): void |
| 63 | + protected function dispatchEvents(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport |
66 | 64 | {
|
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; |
70 | 75 | }
|
71 | 76 | }
|
0 commit comments