Hello,
I need some help to understand the basics of notifications together with broadcasts. I am allready using broadcasts with pusher in my application. Now I want to save these broadcasts in the database. I want that the users see their missed broadcasts when they log in again. I am sure I need notifications for that. But I don't know where to start. I allready have my event which broadcasts a message with pusher. What's next?
Thanks! :)
In your notification class add database to in the via method:
public function via($notifiable)
{
return ['database', 'broadcast'];
}
public function toDatabase($notifiable)
{
return [
// data to be saved in the db
];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
// data to broadcast
]);
}
@HFALUCAS - Thanks for that. One more question: where should I fire the notification from? And should I also fire the broadcasting-event? If so, which one first?
@HFALUCAS - Its working. Only strange thing is that when I send the notification to more than one user, the entries in the database have a wrong id stored.
It's allways the id from the notification and not my data. :(
class DeliverynoteSaved extends Notification implements ShouldQueue
{
use Queueable;
public $deliverynote;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($id)
{
$this->id = $id;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the database representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toDatabase($notifiable)
{
return [
'id' => $this->id
];
}
I call it from the Event using
\Notification::send($users, new \App\Notifications\DeliverynoteSaved($this->id));
You are firing the notification inside the event listener? And inside there you also have the collection of users(or user) to send the notification to?