33namespace Illuminate \Notifications \Channels ;
44
55use Illuminate \Notifications \Notification ;
6+ use Illuminate \Contracts \Events \Dispatcher ;
7+ use Illuminate \Contracts \Broadcasting \ShouldBroadcast ;
8+ use Illuminate \Notifications \Events \DatabaseNotificationCreated ;
69
710class DatabaseChannel
811{
12+ /**
13+ * The event dispatcher.
14+ *
15+ * @var \Illuminate\Contracts\Events\Dispatcher
16+ */
17+ protected $ events ;
18+
19+ /**
20+ * Create a new database channel.
21+ *
22+ * @param \Illuminate\Contracts\Events\Dispatcher $events
23+ * @return void
24+ */
25+ public function __construct (Dispatcher $ events )
26+ {
27+ $ this ->events = $ events ;
28+ }
29+
930 /**
1031 * Send the given notification.
1132 *
@@ -16,7 +37,13 @@ class DatabaseChannel
1637 public function send ($ notifiables , Notification $ notification )
1738 {
1839 foreach ($ notifiables as $ notifiable ) {
19- $ this ->createNotification ($ notifiable , $ notification );
40+ $ databaseNotification = $ this ->createNotification ($ notifiable , $ notification );
41+
42+ if ($ notification instanceof ShouldBroadcast) {
43+ $ this ->events ->fire (new DatabaseNotificationCreated (
44+ $ notifiable , $ notification , $ databaseNotification
45+ ));
46+ }
2047 }
2148 }
2249
@@ -29,15 +56,12 @@ public function send($notifiables, Notification $notification)
2956 */
3057 protected function createNotification ($ notifiable , Notification $ notification )
3158 {
32- $ message = $ notification ->message ($ notifiable );
59+ $ message = $ notification ->toDatabase ($ notifiable );
3360
3461 return $ notifiable ->routeNotificationFor ('database ' )->create ([
62+ 'id ' => $ message ->id ,
3563 'type ' => get_class ($ notification ),
36- 'level ' => $ message ->level ,
37- 'intro ' => $ message ->introLines ,
38- 'outro ' => $ message ->outroLines ,
39- 'action_text ' => $ message ->actionText ,
40- 'action_url ' => $ message ->actionUrl ,
64+ 'data ' => $ message ->data ,
4165 'read ' => false ,
4266 ]);
4367 }
0 commit comments