0
votes

I used the laravel notifications to broadcast the notification, with customized user model App\Models\TUser, and everything works fine, the pusher debug console receive the broadcasted message, but I can't get it in the client side of the project using Laravel echo.

<?php

namespace App\Notifications;

use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Queue\SerializesModels;

class NotifySubscribers extends Notification implements ShouldBroadcast

{
//    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */

    protected $class;

    public function __construct($class)
    {
        $this->class = $class;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['broadcast'];
    }


    public function toBroadcast($notifiable)
    {
        return new BroadcastMessage([
            'lesson_name' => 'test'
        ]);
    }


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


The client side

    Echo.private('App.Models.TUser.' + "{{ auth('front')->id() }}")
        .notification(function (notification) {
            alert('test');
        });


Here the function triggering the code


Route::get('notify/user', function() {
    $user = App\Models\TUser::find(8);
    $user->notify(new \App\Notifications\NotifySubscribers(App\Models\TClass::first()));
});


// channels file

Broadcast::channel('App.Models.TUser.{id}', function ($user, $classID) {;
    return true;
});


1
Welcome to SO .. private need auth have u checked network tab it will automaticallly try to call brodcasting/auth url if not then u need to change you channel settingKamlesh Paul
broadcasting/auth return 302ahmdQader

1 Answers

0
votes

add gurad

Broadcast::channel('App.Models.TUser.{id}', function ($user, $classID) {;
    return true;
},['guards' => ['web', 'auth']]);