【问题标题】:what event-name should be used on broadcasting laravel notifications广播 laravel 通知时应该使用什么事件名称
【发布时间】:2017-01-20 16:44:00
【问题描述】:

我想制作实时 laravel 通知。

为此我做了所有configurations described in docs

我的通知类是:

class NewChangeRegisterStatusNotif extends Notification
{
    use Queueable;


    public function __construct ($status_id, $course_id)
    {
        $this->status = CourseUserStatus::findOrFail($status_id);
        $this->course = Course::findOrFail($course_id)->first(['title']);
    }


    public function via ($notifiable)
    {
        return ['database', 'broadcast'];
    }


    public function toMail ($notifiable)
    {
        return (new MailMessage)
            ->line('The introduction to the notification.')
            ->action('Notification Action', 'https://laravel.com')
            ->line('Thank you for using our application!');
    }


    public function toArray ($notifiable)
    {
        return [
            'message' =>
                'Some Messages'
            ,
            'action'  => '#'
        ];
    }
}

下面的代码,创建一个新的通知:

$user = User::findOrFail($user_id);
$user->notify(new NewChangeRegisterStatusNotif($value, $course_id));

这是绑定和接收推送事件的javascript客户端代码:

<script src="{{asset('link/to/pusher.min.js')}}"></script>
        <script>
            Pusher.log  = function (msg) {
                console.log(msg);
            };
            var pusher  = new Pusher("{{env("PUSHER_KEY")}}");
            var channel = pusher.subscribe('App.User.1');
                 channel.bind('Illuminate\Notifications\Events\BroadcastNotificationCreated', function (data) {
                console.log(data);
            });
        </script>

在Debug Console Pusher中订阅App.User.1成功。

Laravel 日志中也会显示以下消息:

[11:08:31] LOG.info: Broadcasting [Illuminate\Notifications\Events\BroadcastNotificationCreated] on channels [private-App.User.1] with payload:
{
    "message": "Some Message",
    "action": "#",
    "id": "57d9e4dd-09cd-4cd2-ba3d-06f3fcf2af84",
    "type": "App\\Notifications\\NewChangeRegisterStatusNotif",
    "socket": null
}

但问题是channel.bind() 没有被解雇。

如您所见,我将 Illuminate\Notifications\Events\BroadcastNotificationCreated 传递给它并尝试将其转义 Illuminate\\Notifications\\Events\\BroadcastNotificationCreated 但它们都不起作用。

什么是问题?

更新:

我发现尽管 laravel 有 Broadcasting 事件,但它没有发送到 pusher 或者 Pusher 无法接收。

【问题讨论】:

    标签: javascript php pusher laravel-5.3


    【解决方案1】:

    简单地将这个方法添加到你的事件类

    public function broadcastAs()
    {
        return 'event-name';
    }
    

    您可以在 laravel 文档中阅读更多内容 https://laravel.com/docs/5.8/broadcasting#defining-broadcast-events 在“广播名称”部分

    默认情况下,Laravel 将使用事件的类名广播事件。但是,您可以通过在事件上定义一个 broadcastAs 方法来自定义广播名称:

    /**
    * The event's broadcast name.
    *
    * @return string
    */
    public function broadcastAs()
    {
        return 'server.created';
    }
    

    如果您使用 broadcastAs 方法自定义广播名称,则应确保使用前导注册您的侦听器。特点。这将指示 Echo 不要将应用程序的命名空间添加到事件中:

    .listen('.server.created', function (e) {
        //....
    });
    
    【解决方案2】:

    通过以下操作解决了我的问题:

    1- 在 config/broadcasting.php 文件第 18 行,我将 'default' =&gt; env('BROADCAST_DRIVER', 'pusher'), 更改为 'default' =&gt; 'pusher',

    2- 将var channel = pusher.subscribe('App.User.1'); 更改为var channel = pusher.subscribe('private-App.User.{{$AuthUser-&gt;user_id}}');

    3- 将channel.bind('Illuminate\Notifications\Events\BroadcastNotificationCreated' 转义为channel.bind('Illuminate\\Notifications\\Events\\BroadcastNotificationCreated'

    我也遵循了https://stackoverflow.com/a/30396900/498504中描述的所有说明

    【讨论】:

      【解决方案3】:

      您可以登录 Pusher 仪表板,转到调试控制台,然后粘贴从 Laravel 代码触发事件时出现的事件的 JSON 吗?

      我怀疑您看不到它们的原因是因为您需要绑定到 private-App.User.1 而不仅仅是 App.User.1

      【讨论】:

        猜你喜欢
        • 2021-04-17
        • 2018-12-19
        • 1970-01-01
        • 2018-01-06
        • 1970-01-01
        • 1970-01-01
        • 2016-06-12
        • 2017-05-04
        • 2015-07-10
        相关资源
        最近更新 更多