Search code examples
laravellaravel-5pusherlaravel-echopusher-js

Laravel Echo not listening notification


I am trying to get notification with laravel echo but it is not working. i can see all the notification on pusher debug console but not on browser console. No error is showing in browser console.

app.js

Echo.private(`App.User.3`)
    .notification((notification) => {
        console.log(notification.type);
    });

BidOnAuction.php

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\BroadcastMessage;
class BidOnAuction extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    protected $user;
    protected $auction;

    public function __construct($user,$auction)

    {
        $this->user=$user;
        $this->auction=$auction;
    }

    /**
     * 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!');
    // }
    public function toDatabase($notifiable)
    {
        return [
            'user_name' => $this->user->name,
            'auction_title' => $this->auction->price,
        ];
    }
    public function toBroadcast($notifiable)
    {
        return new BroadcastMessage([
            'user_name' => $this->user->name,
            'auction_title' => $this->auction->price,
        ]);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

I tried this answer but it is not working

Laravel Echo not listening

I want to get notification on laravel frontend


Solution

  • Channel i was trying to access was a private channel so only an authorize user was able to access to channel. for that i passed middleware in broadcast route

    Providers/BroadcastServiceProvider.php

    public function boot()
    {
        Broadcast::routes(['middleware' => ['web']]);
    
        require base_path('routes/channels.php');
    }