-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Notification Middleware is called twice #31192
Comments
Why would you define/need both the property and method? Can you share some code? |
@driesvints thanks for the reply. I'm only defining the middleware method, not the property. The steps to replicate are rather simple, you should be able to see the middleware called twice. |
@driesvints Apologies I didn't see you asked for an example. In this example, the handle method of It's a tricky issue to explain so apologies I'm not doing a very good job. Try out the notification middleware feature for yourself, put a <?php
declare(strict_types=1);
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class SomeClass extends Notification implements ShouldQueue
{
use Queueable;
/**
* Get the middleware the job should be dispatched through.
*
* @return array
*/
public function middleware()
{
return [new SomeMiddleware()];
}
/**
* Get the mail representation of the notification.
*
* @param User $notifiable
*
* @return MailMessage
*/
public function toMail($notifiable)
{
//
}
/**
* Get the notification's delivery channels.
*
* @param User $notifiable
*
* @return array
*/
public function via($notifiable)
{
return ['mail', 'database'];
}
} |
Are you using a queue or horizon or similar? |
I think this is an issue across all queue drivers, but at the moment i'm just using |
A fix for this was merged. |
Description:
SendQueuedNotifications
contains a middleware property and a middleware method through theQueueable
trait, causing middleware to be called twice.The NotificationSender will dispatch an instance of
SendQueuedNotifications
and pass the notification middleware with thethrough
method. Thethrough
method exists on theQueueable
trait, which adds the middleware to the property.framework/src/Illuminate/Notifications/NotificationSender.php
Lines 193 to 204 in 8e189a8
The snippet below fires the middleware then eventually handles the sending of the notification. If you take a look at line 80, it's merging a
middleware()
method with amiddleware
property. This merge is what's actually causing duplication of middleware. Becausethrough
was called earlier on theSendQueuedNotifications
class the property contains the middleware, but amiddleware()
method also exists which returns that property's data. Causing the array merge to contain an array of duplicates which then essentially means they'll be called twice.framework/src/Illuminate/Queue/CallQueuedHandler.php
Lines 77 to 86 in 8e189a8
Hopefully this makes sense, you should be able to follow the steps to replicate to see this issue.
Steps To Reproduce:
Create a Notification class, add a
middleware()
method that returns an example middleware. In the handle method simply putdump('called')
. Notify a user and you should see this output twice.I'm happy to submit a PR but really can't think of a nice solution for this, any thoughts?
The text was updated successfully, but these errors were encountered: