首页 > 解决方案 > 如何在 Laravel Echo 中从 Pusher 发出广播通知

问题描述

根据我的情况,当用户向数据库添加空缺时,我正在尝试使用 Laravel Echo 侦听器从推送器发出 Laravel 广播通知。我不需要使用 vue.js 在默认通知栏上显示通知。只是我需要在某些用户向数据库添加空缺时发出警报。但不幸的是,我无法弄清楚。有没有办法做到这一点?

这是我的通知类

<?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;
 use App\Employer;
 use App\Vacancy;

 class vacancyAdd extends Notification
 {
  use Queueable;
  public $vacancy;

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


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

public function toDatabase($notifiable)
{
    return [
      'vacancy' => $this->vacancy      
    ];
}

public function toBroadcast($notifiable)
{
    return new BroadcastMessage([
        'vacancy' => $this->vacancy
    ]);
}
}

这是我的 app.js

require('./bootstrap');

        window.Vue = require('vue');

        /**
         * Next, we will create a fresh Vue application instance and attach it to
         * the page. Then, you may begin adding components to this application
         * or customize the JavaScript scaffolding to fit your unique needs.
         */

        Vue.component('ExampleComponent', require('./components/ExampleComponent.vue'));

        const app = new Vue({
            el: '#app',

            created(){
                Echo.private('App.User.'+window.Laravel.userId).notification((notification) => {
                    console.log('Notification received ', notification);
                    alert(notification);
                });
            }

        });

我的 Pusher 调试控制台

Pusher Debug Screenshot 你能帮我摆脱这个吗?

标签: laravelvue.jspusher

解决方案


推荐阅读