diff --git a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php index 77498ea39874..24958852758b 100644 --- a/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php +++ b/src/Illuminate/Notifications/Events/BroadcastNotificationCreated.php @@ -92,6 +92,10 @@ protected function channelName() */ public function broadcastWith() { + if (method_exists($this->notification, 'broadcastWith')) { + return $this->notification->broadcastWith(); + } + return array_merge($this->data, [ 'id' => $this->notification->id, 'type' => $this->broadcastType(), diff --git a/tests/Notifications/NotificationBroadcastChannelTest.php b/tests/Notifications/NotificationBroadcastChannelTest.php index 033cf3f8b3f3..e4cae95c3219 100644 --- a/tests/Notifications/NotificationBroadcastChannelTest.php +++ b/tests/Notifications/NotificationBroadcastChannelTest.php @@ -88,6 +88,21 @@ public function testNotificationIsBroadcastedNow() $channel = new BroadcastChannel($events); $channel->send($notifiable, $notification); } + + public function testNotificationIsBroadcastedWithCustomAdditionalPayload() + { + $notification = new CustomBroadcastWithTestNotification; + $notification->id = 1; + $notifiable = m::mock(); + + $event = new BroadcastNotificationCreated( + $notifiable, $notification, $notification->toArray($notifiable) + ); + + $data = $event->broadcastWith(); + + $this->assertArrayHasKey('additional', $data); + } } class NotificationBroadcastChannelTestNotification extends Notification @@ -136,3 +151,16 @@ public function toBroadcast() return (new BroadcastMessage([]))->onConnection('sync'); } } + +class CustomBroadcastWithTestNotification extends Notification +{ + public function toArray($notifiable) + { + return ['invoice_id' => 1]; + } + + public function broadcastWith() + { + return ['id' => 1, 'type' => 'custom', 'additional' => 'custom']; + } +}