diff --git a/src/Illuminate/Notifications/NotificationSender.php b/src/Illuminate/Notifications/NotificationSender.php index d64b710530f3..698d19dcb95f 100644 --- a/src/Illuminate/Notifications/NotificationSender.php +++ b/src/Illuminate/Notifications/NotificationSender.php @@ -199,6 +199,12 @@ protected function queueNotification($notifiables, $notification) $notification->locale = $this->locale; } + $connection = $notification->connection; + + if (method_exists($notification, 'viaConnections')) { + $connection = $notification->viaConnections()[$channel] ?? null; + } + $queue = $notification->queue; if (method_exists($notification, 'viaQueues')) { @@ -222,7 +228,7 @@ protected function queueNotification($notifiables, $notification) $this->bus->dispatch( (new SendQueuedNotifications($notifiable, $notification, [$channel])) - ->onConnection($notification->connection) + ->onConnection($connection) ->onQueue($queue) ->delay(is_array($delay) ? ($delay[$channel] ?? null) : $delay) ->through($middleware) diff --git a/tests/Notifications/NotificationSenderTest.php b/tests/Notifications/NotificationSenderTest.php index d0fba449c849..d969e40c532f 100644 --- a/tests/Notifications/NotificationSenderTest.php +++ b/tests/Notifications/NotificationSenderTest.php @@ -104,6 +104,29 @@ public function testItCanSendQueuedMultiChannelNotificationsThroughDifferentMidd $sender->send($notifiable, new DummyMultiChannelNotificationWithConditionalMiddlware); } + + public function testItCanSendQueuedWithViaConnectionsNotifications() + { + $notifiable = new AnonymousNotifiable; + $manager = m::mock(ChannelManager::class); + $bus = m::mock(BusDispatcher::class); + $bus->shouldReceive('dispatch') + ->once() + ->withArgs(function ($job) { + return $job->connection === 'sync' && $job->channels === ['database']; + }); + $bus->shouldReceive('dispatch') + ->once() + ->withArgs(function ($job) { + return $job->connection === null && $job->channels === ['mail']; + }); + + $events = m::mock(EventDispatcher::class); + + $sender = new NotificationSender($manager, $bus, $events); + + $sender->send($notifiable, new DummyNotificationWithViaConnections); + } } class DummyQueuedNotificationWithStringVia extends Notification implements ShouldQueue @@ -154,6 +177,23 @@ public function via($notifiable) } } +class DummyNotificationWithViaConnections extends Notification implements ShouldQueue +{ + use Queueable; + + public function via($notifiable) + { + return ['mail', 'database']; + } + + public function viaConnections() + { + return [ + 'database' => 'sync', + ]; + } +} + class DummyNotificationWithMiddleware extends Notification implements ShouldQueue { use Queueable;