diff --git a/src/Exceptions/CouldNotSendNotification.php b/src/Exceptions/CouldNotSendNotification.php new file mode 100644 index 0000000..7e2c4f2 --- /dev/null +++ b/src/Exceptions/CouldNotSendNotification.php @@ -0,0 +1,22 @@ +getMessage()}", + $exception->getCode(), + $exception + ); + } +} diff --git a/src/FcmChannel.php b/src/FcmChannel.php index b0d667d..d45be07 100644 --- a/src/FcmChannel.php +++ b/src/FcmChannel.php @@ -28,10 +28,10 @@ public function __construct(protected Dispatcher $events, protected Messaging $c // } - /** + /* * * Send the given notification. */ - public function send(mixed $notifiable, Notification $notification): ?Collection + public function send(mixed $notifiable, Notification $notification): ?Collection { $tokens = Arr::wrap($notifiable->routeNotificationFor('fcm', $notification)); @@ -47,14 +47,16 @@ public function send(mixed $notifiable, Notification $notification): ?Collection ->map(fn (MulticastSendReport $report) => $this->checkReportForFailures($notifiable, $notification, $report)); } + + /** * Handle the report for the notification and dispatch any failed notifications. */ protected function checkReportForFailures(mixed $notifiable, Notification $notification, MulticastSendReport $report): MulticastSendReport { Collection::make($report->getItems()) - ->filter(fn (SendReport $report) => $report->isFailure()) - ->each(fn (SendReport $report) => $this->dispatchFailedNotification($notifiable, $notification, $report)); + ->filter(fn(SendReport $report) => $report->isFailure()) + ->each(fn(SendReport $report) => $this->dispatchFailedNotification($notifiable, $notification, $report)); return $report; } diff --git a/src/FcmTopicChannel.php b/src/FcmTopicChannel.php new file mode 100644 index 0000000..0cbdf17 --- /dev/null +++ b/src/FcmTopicChannel.php @@ -0,0 +1,36 @@ +toFcm($notifiable); + + if (! $msg instanceof Message) { + throw CouldNotSendNotification::invalidMessage(); + } + + if (empty($msg->topic)) { + throw CouldNotSendNotification::serviceRespondedWithAnError( + new \Exception('Topic is required.') + ); + } + + try { + $response = $this->client->send($msg); + } catch (\Throwable $e) { + throw CouldNotSendNotification::serviceRespondedWithAnError($e); + } + + return [$response]; + } +} diff --git a/tests/Resources/FcmTopicChannelTest.php b/tests/Resources/FcmTopicChannelTest.php new file mode 100644 index 0000000..cfa6aa3 --- /dev/null +++ b/tests/Resources/FcmTopicChannelTest.php @@ -0,0 +1,34 @@ +shouldReceive('send') + ->once() + ->andReturn(['name' => 'test']); + + $channel = new FcmTopicChannel($messaging); + + $msg = Mockery::mock(Message::class); + $notification = Mockery::mock(Notification::class); + $notification->shouldReceive('toFcm')->andReturn($msg); + + $msg->topic = 'news'; + + $res = $channel->send(null, $notification); + $this->assertEquals([['name' => 'test']], $res); + } +}