在实现队列时,Laravel通知侦听器是无用的
拉威尔版本:5.5* PHP版本:7.1* 根据文档,订阅通知事件应该非常简单。我遵循了文档中的步骤,但是我的通知实现了在实现队列时,Laravel通知侦听器是无用的,laravel,events,laravel-5.5,Laravel,Events,Laravel 5.5,拉威尔版本:5.5* PHP版本:7.1* 根据文档,订阅通知事件应该非常简单。我遵循了文档中的步骤,但是我的通知实现了ShouldQueue,并且没有正确填充事件侦听器。我想知道这个问题是否是 请注意,在框架github中(右上方链接),只从sendToNotifiable函数触发newevents\NotificationSent($notifiable,$notification,$channel,$response),而该函数又只从sendNow函数触发。send函数本身如下: publ
ShouldQueue
,并且没有正确填充事件侦听器。我想知道这个问题是否是
请注意,在框架github中(右上方链接),只从sendToNotifiable
函数触发newevents\NotificationSent($notifiable,$notification,$channel,$response)
,而该函数又只从sendNow
函数触发。send
函数本身如下:
public function send($notifiables, $notification)
{
$notifiables = $this->formatNotifiables($notifiables);
if ($notification instanceof ShouldQueue) {
return $this->queueNotification($notifiables, $notification);
}
return $this->sendNow($notifiables, $notification);
}
也就是说,在我看来,如果是if($notification instanceof ShouldQueue)的情况,则事件不会触发{
asqueueNotification
从不触发事件侦听器。我假设它进入队列,然后需要重新触发事件,但我认为不会发生这种情况,因为我的NotificationSent
侦听器没有使用该类构造函数中的任何数据填充
EventServiceProvider:
protected $listen = [
'Illuminate\Notifications\Events\NotificationSent' => [
'App\Listeners\NewNotificationListener',
],
NewNotificationListener:
<?php
namespace App\Listeners;
use Illuminate\Notifications\Events\NotificationSent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Jobs\SendEmailForNotifications;
use Illuminate\Support\Facades\Log;
class NewNotificationListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
public function handle(NotificationSent $event)
{
Log:info('Notification Listener:'.' '.var_dump($event));
SendEmailForNotifications::dispatch($event->notification)->delay(now()->addMinutes(10));
}
}
快速回答:进行这些修改时是否重新启动了队列工作程序?
“我的盒子”上的NotificationSent
在排队和处理时会按预期触发和捕获
当Laravel在通知发送者中点击此代码时:
if ($notification instanceof ShouldQueue) {
return $this->queueNotification($notifiables, $notification);
}
它使用队列调度器对通知进行排队并将其存储到您的队列中。当您的工作人员拾取该通知时,它将取消序列化该命令,并启动SendQueuedNotifications
。然后该类将处理排队通知,并处理队列():
而ChannelManager
会这样做():
这样就可以了。调用了NotificationSender
中的sendNow
。应该在此函数中调用notificationsend
事件
编辑
我就是这样测试的:
确保您的队列设置正确。我使用数据库队列和jobs/failed_jobs表组合
创建文件app/Listeners/TestListener.php
<?php
namespace App\Listeners;
use Illuminate\Notifications\Events\NotificationSent;
class TestListener
{
public function handle(NotificationSent $event)
{
\Log::info(get_class($event));
}
}
<?php
namespace App\Providers;
use App\Listeners\TestListener;
use Illuminate\Notifications\Events\NotificationSent;
use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
NotificationSent::class => [
TestListener::class
]
];
}
创建虚拟通知(发送Hello电子邮件):
检查laravel.log
,您应该在那里打印NotificationSent
的类名
[2018-03-06 09:51:02] production.INFO: Illuminate\Notifications\Events\NotificationSent
当您说“我的盒子”上的NotificationSent
在排队和处理时会按预期触发和捕获,您是否能够在侦听器handle()中填充$event
function?是的,我在handle
的第一个参数中得到了NotificationSent
事件的一个实例。为了您的信息,我做了var\u dump($event)
,它在运行队列的控制台中打印输出,但不在laravel日志中。试着这样做:log:info('Notification Listener:'。'var\u dump'($event->channel));
,例如,无论我记录什么,$event
或其属性都不起作用。你能发布你的代码吗,这样我就可以准确地复制它,看看它是否起作用……也许我的环境出了问题。。。
<?php
namespace App\Providers;
use App\Listeners\TestListener;
use Illuminate\Notifications\Events\NotificationSent;
use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
NotificationSent::class => [
TestListener::class
]
];
}
<?php
namespace App\Notifications\Users;
use App\Notifications\Notification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Channels\MailChannel;
use Illuminate\Notifications\Messages\MailMessage;
class WelcomeNotification extends Notification implements ShouldQueue
{
use Queueable;
public function via($notifiable)
{
return [MailChannel::class];
}
public function toMail($notifiable)
{
return (new MailMessage())
->line('Hello');
}
}
$user->notify(new WelcomeNotification());
[2018-03-06 09:51:02] production.INFO: Illuminate\Notifications\Events\NotificationSent