【问题标题】:laravel Echo doesn't receive private channel notification with pusherlaravel Echo 没有收到带有推送器的私人频道通知
【发布时间】:2020-09-22 06:12:34
【问题描述】:

我使用 laravel 通知广播通知,自定义用户模型 App\Models\TUser,一切正常,推送器调试控制台收到广播消息,但我无法在客户端获取它使用 Laravel 回显的项目。

<?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;
});


【问题讨论】:

  • Welcome to SO .. private need auth has you check network tab it will automatically all tr​​y to call brodcasting/auth url if not then you need to change you channel setting
  • 广播/授权返回 302

标签: laravel pusher


【解决方案1】:

添加gurad

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

【讨论】:

    猜你喜欢
    • 2018-01-02
    • 2020-04-14
    • 2018-01-21
    • 2017-10-09
    • 2018-04-25
    • 1970-01-01
    • 2020-01-15
    • 2017-01-21
    • 1970-01-01
    相关资源
    最近更新 更多