Laravel 5.5通知实现ShouldQueue时监听器失效问题咨询
Hey there, let's break down how to fix this issue with your queued Laravel 5.5 notifications not firing the NotificationSent event properly:
First, let's align on core behavior: When your notification implements ShouldQueue, the NotificationSent event doesn't fire immediately when you dispatch the notification—it fires after the queue worker processes the notification task. That's a key detail easy to overlook!
Here's your step-by-step debug plan:
Verify your queue is actually running
If you haven't started a queue worker, the notification will sit in your queue storage (database/Redis/etc.) forever, and the event will never trigger. Run this command in your terminal:php artisan queue:workAlso double-check your
.envqueue driver config (e.g.,QUEUE_DRIVER=database) matches your setup, and ensure there are no failed tasks clogging things up (check thefailed_jobstable if using database driver).Check for serialization issues in your notification class
Queued notifications get serialized before being pushed to the queue. If your notification has properties that can't be serialized (like closures, resource objects, or un-serializable custom classes), those properties will be lost by the time the queue worker processes it—resulting in empty data in yourNotificationSentevent.
Make sure all properties in your notification class are primitive types (strings, integers, arrays) or implement theSerializableinterface if they're custom objects.Confirm your event listener is registered correctly
Head to yourapp/Providers/EventServiceProvider.phpand ensure you've mapped theNotificationSentevent to your listener:protected $listen = [ \Illuminate\Notifications\Events\NotificationSent::class => [ \App\Listeners\YourNotificationListener::class, ], ];If you just added this mapping, run
php artisan event:generateto auto-create the listener file (if you didn't build it manually).Debug with logging to see what data is actually coming through
In your listener'shandle()method, add a log entry to inspect the event data:use Illuminate\Support\Facades\Log; public function handle(\Illuminate\Notifications\Events\NotificationSent $event) { Log::info('Notification Sent Event Data', [ 'notifiable_id' => $event->notifiable->id ?? 'null', 'notification_class' => get_class($event->notification), 'channel' => $event->channel, 'response' => $event->response ]); }Check your
storage/logs/laravel.logfile after the queue worker runs—this will tell you exactly which data is missing or null.Note on event execution context
Remember: When using queued notifications, theNotificationSentevent fires in the queue worker's process, not the original request process. If you were expecting to catch the event during the initial request, that won't happen—you'll need to handle it asynchronously alongside the queued task.
内容的提问来源于stack exchange,提问作者Summer Developer

