【问题标题】:Laravel 5.4 Notification BroadcastingLaravel 5.4 通知广播
【发布时间】:2018-01-06 07:39:19
【问题描述】:

我已经用数据库设置了它,并且所有东西都正确插入了那里。 我用 Laravel Echo 正确收听,并且这被记录在 Pusher 上,但是我的所有通知等都没有被 pusher 接收到?谁能看到我做错了什么? 有人帮我plzzzzzzzz吗?

我的通知类

<?php

namespace App\Notifications;
use Carbon\Carbon;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\BroadcastMessage;

use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class RepliedToThread extends Notification 
{
    use Queueable;
    public $thread;
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($thread)
    {
        $this->thread=$thread;
    }

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

  
    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    // public function toDatabase($notifiable)
    // {
    //     return [
    //            'thread' => $this->thread, 
    //            'repliedToTime' =>Carbon::now(),
    //            'user'=>auth()->user()


    //     ];
    // }
    
    // public function toBroadcast($notifiable)
    // {
    //     return new BroadcastMessage([
    //                 'thread' => $this->thread, 
    //                'repliedToTime' =>Carbon::now(),
    //                'user'=>auth()->user(),
    //     ]);
    // }
    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
                    'thread' => $this->thread, 
                   'repliedToTime' =>Carbon::now(),
                   'user'=>auth()->user(),
        ];
    }
}

broadcating.php

虽然当它被触发时它被发送到数据库而不是推送器。 pusher 的所有内容都设置在我的 .env 文件中。

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Broadcaster
    |--------------------------------------------------------------------------
    |
    | This option controls the default broadcaster that will be used by the
    | framework when an event needs to be broadcast. You may set this to
    | any of the connections defined in the "connections" array below.
    |
    | Supported: "pusher", "redis", "log", "null"
    |
    */

    'default' => "pusher",

    /*
    |--------------------------------------------------------------------------
    | Broadcast Connections
    |--------------------------------------------------------------------------
    |
    | Here you may define all of the broadcast connections that will be used
    | to broadcast events to other systems or over websockets. Samples of
    | each available type of connection are provided inside this array.
    |
    */

    'connections' => [

        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => 'ap1',
                'encrypted' => true
            ],
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],

        'log' => [
            'driver' => 'log',
        ],

        'null' => [
            'driver' => 'null',
        ],

    ],

];

.env 文件

BROADCAST_DRIVER="pusher"
PUSHER_KEY="public_key"
PUSHER_SECRET="secret_key"
PUSHER_APP_ID=app_id

bootstrap.js

window._ = require('lodash');

/**
 * We'll load jQuery and the Bootstrap jQuery plugin which provides support
 * for JavaScript based Bootstrap features such as modals and tabs. This
 * code may be modified to fit the specific needs of your application.
 */

try {
    window.$ = window.jQuery = require('jquery');

    require('bootstrap-sass');
} catch (e) {}

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

/**
 * Next we will register the CSRF Token as a common header with Axios so that
 * all outgoing HTTP requests automatically have it attached. This is just
 * a simple convenience so we don't have to attach every token manually.
 */

let token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

// window.axios.defaults.headers.common = {
//     // 'X-CSRF-TOKEN': window.Laravel.csrfToken, <-- Comment it out (if you are extending layouts.app file, you won't require this.)
//     'X-Requested-With': 'XMLHttpRequest'
// };
import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');
window.Echo = new Echo({
	
    broadcaster: 'pusher',
    key: '################',
    cluster: 'ap1',
    encrypted : true
});
/**
 * Echo exposes an expressive API for subscribing to channels and listening
 * for events that are broadcast by Laravel. Echo and event broadcasting
 * allows your team to easily build robust real-time web applications.
 */

// import Echo from 'laravel-echo'

// window.Pusher = require('pusher-js');

// window.Echo = new Echo({
// 	authEndpoint : 'http://localhost/forum_web/public/broadcasting/auth',

//     broadcaster: 'pusher',
//     key: '9964dcd35bae49f32d6c',
//     cluster: 'eu',
//     encrypted: true,
// });
我用过vue2

notification.vue

<template>
    <li class="dropdown" @click="markNotificationAsRead">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
            <span class="glyphicon glyphicon-globe"></span> Notifications <span
                class="badge alert-danger">{{unreadNotifications.length}}</span>
        </a>

        <ul class="dropdown-menu" role="menu">
            <li>
                <notification-item v-for="unread in unreadNotifications" :unread="unread"></notification-item>
            </li>
        </ul>
    </li>
</template>

<script>
    import NotificationItem from './NotificationItem.vue';
    export default {
        props: ['unreads', 'userid'],
        components: {NotificationItem},
        data(){
            return {
                unreadNotifications: this.unreads
            }
        },
        methods: {
            markNotificationAsRead() {
                if (this.unreadNotifications.length) {
                    axios.get('markAsRead');
                }
            }
        },
        mounted() {
            console.log('Component mounted. asd 1111');
            Echo.private('App.User.' + this.userid)
                .notification((notification) => {
                    console.log(notification);
                    
                });

        }
    }
</script>

【问题讨论】:

  • 触发事件的部分在哪里?
  • 查看notification.vue in mount
  • 啊,没注意到。对不起

标签: php laravel laravel-5 vue.js laravel-5.3


【解决方案1】:

我发现了错误
我下载的节点是旧版本和 npm 将其完整并重新安装 工作过

【讨论】:

  • 如果我们为消息通知添加新的下拉菜单会怎样?
猜你喜欢
  • 2017-12-26
  • 2017-07-06
  • 2017-07-04
  • 2021-04-17
  • 2017-12-12
  • 1970-01-01
  • 2018-07-13
  • 2021-11-19
  • 2018-09-22
相关资源
最近更新 更多