Hello,I’m really confused about laravel and notifcations in combination with livewire and echo / pusher. I’m using this package to manage friendships between users (among other things)
https://github.com/multicaret/laravel-acquaintances
.
I have a livewire component where users can search for other users to send them a friend request,
<button wire:click="sendFriendRequest({{$user->id}})" class="w-24 text-right cursor-pointer flex justify-end">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd"
d="M10 18a8 8 0 100-16 8 8 0 000 16zm1-11a1 1 0 10-2 0v2H7a1 1 0 100 2h2v2a1 1 0 102 0v-2h2a1 1 0 100-2h-2V7z"
clip-rule="evenodd"/>
</svg>
</button>
The function sendFriendRequest is called on the button click to send a request. The befriend
method is part of the package, I would like to notify said user who was send a friend request, via a notification.
public function sendFriendRequest(int $id)
{
Auth::user()->befriend(User::find($id)->first());
Auth::user()->notify(new SendFriendRequest());
}
The notifiable class
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class SendFriendRequest extends Notification
{
use Queueable;
public $friendRequest;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database', 'broadcast'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
public function toDataBase($notifiable)
{
return [
'name' => $notifiable->name,
'message' => 'Has send you a friend request',
];
}
}
And here is where my confussion starts. I read the laravel docs about notifications, and the livewire docs about using laravel echo in combination with livewire. but it seems livewire will only work with events? and not notifcations, so is it somehow possible to use livewire to listen to a notification event ? in the $listeners
property ? and then send a notifcation to the user? I’m sorry if this is a bit confusing as this is my first time working with these sort of things, but the offical laravel and livewire docs are all very unclear about this sort of thing, and the laravel docs contain so much information that I’m honestly a bit overwhelmed about how to handle this.
Laravel echo config
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
forceTLS: true
});