1

在我的网络应用程序中,我想在从管理员创建某些任务时通知一些特定用户。这应该是实时的。因此我使用了 laravel 推送器。但这没有任何意义。我想解决这个问题。

我的控制器:

        public function add(Request $request){
            $data = $request->all();
            $tasks = Todo::create($data);

            //send notofications 
            $notifyTo = User::whereHas('roles', function($q){$q->whereIn('slug', [ 'manager'   ]);})->get();
            foreach ($notifyTo as $notifyUser) {
                $notifyUser->notify(new TaskCreated($tasks));
            }
        }

我的 TaskCreated 通知类

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

public function toBroadcast($notifiable)
{
    return [
        'title' => $this->tasks->task
    ];
}

我的 Pusher 调试控制台

Pusher 调试控制台

这是我的 boostrap.js

/**
 * 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({
    broadcaster: 'pusher',
    key:'bf6e79cce8gfggf548fb2c5e9',
    cluster: 'ap2',
    forceTLS: true
});

这是我的前端

<script>
  var userId = $('meta[name="userId"]').attr('content');
    Echo.private('App.User.' + userId)
    .notification((notification) => {
        console.log(notification.type);
    });

  </script>

配置/广播.php

<?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' => env('BROADCAST_DRIVER', 'null'),

    /*
    |--------------------------------------------------------------------------
    | 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' => env('PUSHER_APP_CLUSTER'),
                //'encrypted' => true,
               // 'host' => '127.0.0.1',
               // 'port' => 6004,
               // 'scheme' => 'http'
            //],

            'options' => [
                'cluster' => 'ap2',
                'useTLS' => true
            ],
        ],

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

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

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

    ],

];

我想在我的 laravel 网络应用程序中提醒通知标题。怎么可能?请提出前端的解决方案。

4

1 回答 1

2

您需要Notifiable在用户模型中使用特征。之后,您需要指定广播将使用的频道。要使其唯一,请在常量channel_name的末尾附加user_id ,如下所示:

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The channels the user receives notification broadcasts on.
     *
     * @return string
     */
    public function receivesBroadcastNotificationsOn()
    {
        return 'users.'.$this->id;
    }
}

在前端,您需要使用一个名为Laravel Echo. 你可以使用Laravel Echo在前端收听广播,如下所示:

Echo.private('users.' + userId)
    .notification((notification) => {
        console.log(notification.type);
    });

参考文档:https ://laravel.com/docs/7.x/notifications#listening-for-notifications

于 2020-05-05T12:56:17.740 回答