如何不在队列中添加通知广播?拉拉韦尔5.8

时间:2019-10-14 13:12:22

标签: laravel pusher

我想要实现的是立即广播通知,而不需要运行

  

php artisan queue:listen

如果在Event类中,我只需要实现ShouldBroadcastNow即可立即广播事件。但是我在Notification中尝试了相同的方法,但是没有用。

通知

<?php

namespace App\Notifications;

use App\Tournament;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Messages\MailMessage;

class TournamentAdded extends Notification implements ShouldBroadcastNow
{
    use Queueable;

    protected $tournament;
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(Tournament $tournament)
    {
        $this->tournament = $tournament;
    }

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

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->line('The introduction to the notification.')
            ->action('Notification Action', url('/'))
            ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            $this->tournament
        ];
    }
    public function toBroadcast($notifiable)
    {
        return new BroadcastMessage([
            $this->tournament
        ]);
    }
}

执行通知

 $t = Tournament::latest()->first();
 $user->notify(new AppTournamentAdded($t));

0 个答案:

没有答案