diff --git a/src/Illuminate/Notifications/Channels/BroadcastChannel.php b/src/Illuminate/Notifications/Channels/BroadcastChannel.php index 55c4d1f3f9a6..cb3f860581e3 100644 --- a/src/Illuminate/Notifications/Channels/BroadcastChannel.php +++ b/src/Illuminate/Notifications/Channels/BroadcastChannel.php @@ -5,6 +5,7 @@ use RuntimeException; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Notifications\Messages\BroadcastMessage; use Illuminate\Notifications\Events\BroadcastNotificationCreated; class BroadcastChannel @@ -36,9 +37,17 @@ public function __construct(Dispatcher $events) */ public function send($notifiable, Notification $notification) { - return $this->events->fire(new BroadcastNotificationCreated( - $notifiable, $notification, $this->getData($notifiable, $notification) - )); + $message = $this->getData($notifiable, $notification); + + $event = new BroadcastNotificationCreated( + $notifiable, $notification, is_array($message) ? $message : $message->data + ); + + if ($message instanceof BroadcastMessage) { + $event->connection = 'sync'; + } + + return $this->events->fire($event); } /** @@ -46,7 +55,7 @@ public function send($notifiable, Notification $notification) * * @param mixed $notifiable * @param \Illuminate\Notifications\Notification $notification - * @return array + * @return mixed * * @throws \RuntimeException */ diff --git a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php index 644a46cc83b4..2dc851ce1e1d 100644 --- a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php +++ b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php @@ -24,6 +24,13 @@ class BroadcastNotificationCreated implements ShouldBroadcast */ public $notification; + /** + * The queue connection. + * + * @var string + */ + public $connection; + /** * The notification data. * diff --git a/src/Illuminate/Notifications/Messages/BroadcastMessage.php b/src/Illuminate/Notifications/Messages/BroadcastMessage.php new file mode 100644 index 000000000000..6c6a06e6cb8d --- /dev/null +++ b/src/Illuminate/Notifications/Messages/BroadcastMessage.php @@ -0,0 +1,56 @@ +data = $data; + } + + /** + * Set the message data. + * + * @param array $data + * @return $this + */ + public function data($data) + { + $this->data = $data; + + return $this; + } + + /** + * Broadcast this message immediately. + * + * @return $this + */ + public function now() + { + $this->broadcastNow = true; + + return $this; + } +} diff --git a/tests/Notifications/NotificationBroadcastChannelTest.php b/tests/Notifications/NotificationBroadcastChannelTest.php index 440432b5f2c4..98b33e1f2b11 100644 --- a/tests/Notifications/NotificationBroadcastChannelTest.php +++ b/tests/Notifications/NotificationBroadcastChannelTest.php @@ -37,6 +37,20 @@ public function testNotificationIsBroadcastedOnCustomChannels() $this->assertEquals(new PrivateChannel('custom-channel'), $channels[0]); } + + public function testNotificationIsBroadcastedNow() + { + $notification = new TestNotificationBroadCastedNow; + $notification->id = 1; + $notifiable = Mockery::mock(); + + $events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher'); + $events->shouldReceive('fire')->once()->with(Mockery::on(function ($event) { + return $event->connection == 'sync'; + })); + $channel = new BroadcastChannel($events); + $channel->send($notifiable, $notification); + } } class NotificationBroadcastChannelTestNotification extends Notification @@ -59,3 +73,16 @@ public function broadcastOn() return [new PrivateChannel('custom-channel')]; } } + +class TestNotificationBroadCastedNow extends Notification +{ + public function toArray($notifiable) + { + return ['invoice_id' => 1]; + } + + public function toBroadcast() + { + return (new \Illuminate\Notifications\Messages\BroadcastMessage([]))->now(); + } +}