Skip to content

Commit

Permalink
Return collection of responses from send
Browse files Browse the repository at this point in the history
  • Loading branch information
dwightwatson committed Dec 24, 2023
1 parent c60af3e commit ffd690f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
30 changes: 8 additions & 22 deletions src/FcmChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Collection;
use Illuminate\Support\Arr;
use Kreait\Firebase\Contract\Messaging;
use Kreait\Firebase\Messaging\MulticastSendReport;
Expand All @@ -21,9 +22,6 @@ class FcmChannel

/**
* Create a new channel instance.
*
* @param Illuminate\Contracts\Events\Dispatcher $events
* @param Firebase\Contract\Messaging $client
*/
public function __construct(protected Dispatcher $events, protected Messaging $client)
{
Expand All @@ -32,51 +30,39 @@ public function __construct(protected Dispatcher $events, protected Messaging $c

/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification): void
public function send(mixed $notifiable, Notification $notification): ?Collection
{
$tokens = Arr::wrap($notifiable->routeNotificationFor('fcm', $notification));

if (empty($tokens)) {
return;
return null;
}

$fcmMessage = $notification->toFcm($notifiable);

collect($tokens)
return collect($tokens)
->chunk(self::TOKENS_PER_REQUEST)
->map(fn ($tokens) => ($fcmMessage->client ?? $this->client)->sendMulticast($fcmMessage, $tokens->all()))
->map(fn (MulticastSendReport $report) => $this->checkReportForFailures($notifiable, $notification, $report));
}

/**
* Handle the report for the notification and dispatch any failed notifications.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param Kreait\Firebase\Messaging\MulticastSendReport. $report
* @return void
*/
protected function checkReportForFailures($notifiable, $notification, MulticastSendReport $report)
protected function checkReportForFailures(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport
{
collect($report->getItems())
->filter(fn (SendReport $report) => $report->isFailure())
->each(fn (SendReport $report) => $this->dispatchFailedNotification($notifiable, $notification, $report));

return $report;
}

/**
* Dispatch failed event.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @param \Kreait\Firebase\Messaging\SendReport $report
* @return void
*/
protected function dispatchFailedNotification($notifiable, Notification $notification, SendReport $report): void
protected function dispatchFailedNotification(mixed $notifiable, Notification $notification, SendReport $report): void
{
$this->events->dispatch(new NotificationFailed($notifiable, $notification, self::class, [
'report' => $report,
Expand Down
8 changes: 5 additions & 3 deletions tests/FcmChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Collection;
use Kreait\Firebase\Contract\Messaging;
use Kreait\Firebase\Messaging\MessageTarget;
use Kreait\Firebase\Messaging\MulticastSendReport;
Expand Down Expand Up @@ -48,7 +49,8 @@ public function test_it_can_send_notifications()

$result = $channel->send(new DummyNotifiable, new DummyNotification);

$this->assertNull($result);
$this->assertInstanceOf(Collection::class, $result);
$this->assertInstanceOf(MulticastSendReport::class, $result->first());
}

public function test_it_can_send_notifications_with_custom_client()
Expand All @@ -70,7 +72,7 @@ public function test_it_can_send_notifications_with_custom_client()

$result = $channel->send(new DummyNotifiable, new DummyNotification($customFirebase));

$this->assertNull($result);
$this->assertInstanceOf(Collection::class, $result);
}

public function test_it_can_dispatch_events()
Expand All @@ -89,7 +91,7 @@ public function test_it_can_dispatch_events()

$result = $channel->send(new DummyNotifiable, new DummyNotification);

$this->assertNull($result);
$this->assertInstanceOf(Collection::class, $result);
}

protected function target()
Expand Down

0 comments on commit ffd690f

Please sign in to comment.