|
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; |
9 | 7 | use Illuminate\Notifications\Notification; |
10 | 8 | use Illuminate\Support\Arr; |
11 | 9 | use Illuminate\Support\Collection; |
@@ -37,40 +35,37 @@ public function send(mixed $notifiable, Notification $notification): ?Collection |
37 | 35 | { |
38 | 36 | $tokens = Arr::wrap($notifiable->routeNotificationFor('fcm', $notification)); |
39 | 37 |
|
| 38 | + if (empty($tokens)) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + $fcmMessage = $notification->toFcm($notifiable); |
| 43 | + |
40 | 44 | return Collection::make($tokens) |
41 | 45 | ->chunk(self::TOKENS_PER_REQUEST) |
42 | | - ->map(fn ($tokens) => $this->sendNotifications($notifiable, $notification, $tokens)) |
43 | | - ->map(fn (MulticastSendReport $report) => $this->dispatchEvents($notifiable, $notification, $report)); |
| 46 | + ->map(fn ($tokens) => ($fcmMessage->client ?? $this->client)->sendMulticast($fcmMessage, $tokens->all())) |
| 47 | + ->map(fn (MulticastSendReport $report) => $this->checkReportForFailures($notifiable, $notification, $report)); |
44 | 48 | } |
45 | 49 |
|
46 | 50 | /** |
47 | | - * Send the notification with the provided tokens. |
| 51 | + * Handle the report for the notification and dispatch any failed notifications. |
48 | 52 | */ |
49 | | - protected function sendNotifications(mixed $notifiable, Notification $notification, Collection $tokens): MulticastSendReport |
| 53 | + protected function checkReportForFailures(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport |
50 | 54 | { |
51 | | - $fcmMessage = $notification->toFcm($notifiable); |
52 | | - |
53 | | - $this->events->dispatch( |
54 | | - new NotificationSending($notifiable, $notification, self::class) |
55 | | - ); |
| 55 | + Collection::make($report->getItems()) |
| 56 | + ->filter(fn (SendReport $report) => $report->isFailure()) |
| 57 | + ->each(fn (SendReport $report) => $this->dispatchFailedNotification($notifiable, $notification, $report)); |
56 | 58 |
|
57 | | - return ($fcmMessage->client ?? $this->client)->sendMulticast($fcmMessage, $tokens->all()); |
| 59 | + return $report; |
58 | 60 | } |
59 | 61 |
|
60 | 62 | /** |
61 | | - * Handle the report for the notification and dispatch any failed notifications. |
| 63 | + * Dispatch failed event. |
62 | 64 | */ |
63 | | - protected function dispatchEvents(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport |
| 65 | + protected function dispatchFailedNotification(mixed $notifiable, Notification $notification, SendReport $report): void |
64 | 66 | { |
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; |
| 67 | + $this->events->dispatch(new NotificationFailed($notifiable, $notification, self::class, [ |
| 68 | + 'report' => $report, |
| 69 | + ])); |
75 | 70 | } |
76 | 71 | } |
0 commit comments