提问者:小点点

laravel Echo未接收到带有推送器的专用通道通知


我使用laravel通知来广播通知,使用自定义的用户模型App\Models\TUser,一切正常,pusher调试控制台接收广播的消息,但我无法使用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'
        ];
    }
}


客户端

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


这里是触发代码的函数


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个答案

匿名用户

添加gurad

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