I did something similar, implemented as follows:
- Email, SMS, etc. best done through a new feature that appeared in version 5.3: Laravel Notifications
If in the future you want more than just an email notification, check out the site - http://laravel-notification-channels.com/ there are many channels for notifications.
- Notifications in the browser are best implemented through a new feature of version 5.3: Event Broadcasting
Let's start with the fact that we will choose a driver for Event Broadcasting, I will show Pusher as an example.
To begin, make changes to the .env file:
BROADCAST_DRIVER=pusher PUSHER_APP_ID=#данные с панели Pusher PUSHER_KEY=#данные с панели Pusher PUSHER_SECRET=#данные с панели Pusher
Download the package through Composer:
composer require pusher/pusher-php-server
While with Pusher everything, in the end we will return to it again.
Create an event (examples and description are here ) for example, we will send a notification when someone writes to the user. The notification object will look something like this:
<?php namespace App\Events; use Illuminate\Broadcasting\Channel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Queue\SerializesModels; class ChatMessageSend implements ShouldBroadcast { use SerializesModels; /** * @var User */ public $user; /** * @var array */ public $data; /** * Create a new event instance. * @param array $data */ public function __construct(User $user, array $data) { $this->user = $user; $this->data = $data; } /** * Get the channels the event should broadcast on. * * @return Channel|array */ public function broadcastOn() { return ['chat-channel']; } public function broadcastAs() { return 'message-received'; } }
The broadcastOn and broadcastAs methods are needed for Event Broadcasting, you also need to create a Listener for this event in order to send Email and other messages in it.
Now you need to create Mail Notification in which we specify a letter template, an example is available by reference, let's call it for example MessageSend.
Next, you need to create a Listener that will trigger this notification:
<?php namespace App\Listeners; use App\Events\ChatMessageSend; use App\Notifications\MessageSend; class SendChatMessageNotification { /** * Create the event listener. */ public function __construct() { // } /** * Handle the event. * * @param ChatMessageSend $event * @return void */ public function handle(ChatMessageSend $event) { $event->user->notify(new MessageSend($event->data)); } }
Now the EventServiceProvider file EventServiceProvider link the event and the listener
protected $listen = [ 'App\Events\ChatMessageSend' => [ 'App\Listeners\SendChatMessageNotification', ], ];
Now everything is ready for sending notifications. To send a notification, you need to trigger an event:
event(new ChatMessageSend($user, $data));
The e-mail went away, and went to the Pusher service, but now we need to display it in a browser, there are examples for different languages on the Pusher website, I’ll output for JS:
<!DOCTYPE html> <head> <title>Pusher Test</title> <script src="https://js.pusher.com/4.0/pusher.min.js"></script> <script> // Enable pusher logging - don't include this in production Pusher.logToConsole = true; var pusher = new Pusher('key', { cluster: 'eu', encrypted: true }); var channel = pusher.subscribe('chat-channel'); channel.bind('message-received', function(data) { alert(data.data); }); </script> </head>
Instead of alert, write your JS for browser notifications.
PS The code should be working, but could somewhere miss something