laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 31 forks source link

[Proposal] Easy way to cancel notifications at time of sending #2648

Open gbradley opened 3 years ago

gbradley commented 3 years ago

There are many scenarios where it is useful to check for certain conditions before sending a queued notification.

For example, imagine a user marking an order as "dispatched", which queues an OrderDispatched notification. If the order was marked as dispatched by mistake, there's currently no easy way to prevent the notification being sent to the appropriate channels.

There are two workarounds I know of: a) queue a Job instead, performing checks inside handle() and then dispatch the notification synchronously, or b) create a method on the notification, listen for NotificationSending events, and check for the existence of the method on the notification before it is sent.


My suggestion would be to add a new shouldBeSent($channel, $notifiable) method which would be called inside the Illuminate\Notifications\NotificationSender::shouldSendNotification() method. This allows us to add easily add custom logic for cancelling notifictions when data has changed since dispatch, without having to define a custom listener.