Reputation: 81
I configured pusher
, Laravel Broadcasting
, Laravel Echo
to send notifications to my users. It is working fine for the single private channel.
I have Two Private Channels.
But Notifications are passed only through App.User{id}
Channel
How can I send notifications from other channel too ?
My User Class
namespace App;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The channels the user receives notification broadcasts on.
*
* @return string
*/
public function receivesBroadcastNotificationsOn()
{
return 'App.User.'.$this->id;
return 'YouthAdd.YouthAdd.'.$this->id;
}
}
My Channels.php Route file
Broadcast::channel('App.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Broadcast::channel('YouthAdd.YouthAdd.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
My Front End
<script>
var userId = $('meta[name="userId"]').attr('content');
Echo.private('App.User.' + userId)
.notification((notification) => {
toastr.warning(notification.title, notification.name, {closeButton: true, timeOut: 5000000});
});
Echo.private('YouthAdd.YouthAdd.' + userId)
.notification((notification) => {
console.log(notification.youth);
toastr.error(notification.youth.name, notification.youth.nic, {closeButton: true, timeOut: 5000000});
});
</script>
Anyone can answer this ?
Upvotes: 1
Views: 2647
Reputation: 1101
This works for me.
$channels = array();
foreach ($this->athletes as $athlete) {
array_push($channels, new PrivateChannel('athlete.' . $athlete->user->id));
}
array_push($channels, new PrivateChannel('user.' . $this->user->id));
return $channels;
Upvotes: 0
Reputation: 919
Try this:
$channelNames = ['App.User.' . $this->id, 'YouthAdd.YouthAdd.'.$this->id];
$channels = [];
foreach ($channelNames as $channel) {
$channels[] = new PrivateChannel($channel);
}
return $channels;
Upvotes: 0
Reputation: 7240
instead of :
public function receivesBroadcastNotificationsOn()
{
return 'App.User.'.$this->id;
return 'YouthAdd.YouthAdd.'.$this->id;
}
try this:
public function receivesBroadcastNotificationsOn()
{
return [
'App.User.'.$this->id,
'YouthAdd.YouthAdd.'.$this->id
];
}
Upvotes: 1