Skip to content

Commit

Permalink
feat: Notification delayed blocking after the affect
Browse files Browse the repository at this point in the history
  • Loading branch information
craigAtCD committed Jul 1, 2021
1 parent 78830a6 commit 37321e6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,46 @@ $app->make(Action::class)->execute(...)
If you discover any security related issues, please email
instead of using the issue tracker.

## Delayed notifications blocking:

using this package you can setup your notifications to stop - wheter using feature flags or some other reason -
Whenever a notification is being handled in Laravel the `NotificationSending` event is first emitted. We can use this event to once again check if the notification should be sent, thanks to `SerializesModels` and our `blockSend` method.

if the `blockSend` method returns true, we stop the notification as it is no longer needed.

### Steps

1. setup your notification -

```php
class TaskReminder extends Notification implements ShouldQueue
{
use Queueable, SerializesModels;

public $task;

public function __construct(Task $task)
{
$this->task = $task;

$this->delay($task->start_date_time)->subDay(1);
}

public function vai(){
return ['slack'];
}

/** NEW METHOD HERE FOR STOPPING THE NOTIFICATION **/
public function blockSend($notifiable): bool
{
return $this->task->isCompleted() || $this->task->isCancelled(); //prevent if complete or cancelled
//Feature Flag Example
return Feature::disabled('trigger_task_reminder_via_slack'); //Feature Flag Example
}
```

By listening to that event and then checking our `dontblockSendSend()` method on our notification, we can do in-the-moment checks as the notification is being handled to see if it’s still valid. You can even access the `$notifiable` object, which allows you to check fresh data about the user or entity you’re notifying.

## Credits

- [](https://github.com/custom-d/laravel-helpers)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
],
"require": {
"php": ">=7.2",
"illuminate/support": "^6.0|^7.0|^8.0"
"illuminate/support": "^6.0|^7.0|^8.0",
"illuminate/notifications": "^6.0|^7.0|^8.0"
},
"require-dev": {
"orchestra/testbench": "^4.0|^5.0|^6.0",
Expand Down
16 changes: 16 additions & 0 deletions src/EventMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace CustomD\LaravelHelpers;

use Illuminate\Notifications\Events\NotificationSending;
use CustomD\LaravelHelpers\Listeners\NotificationSendingListener;

trait EventMap
{

protected $listen = [
NotificationSending::class => [
NotificationSendingListener::class,
],
];
}
17 changes: 17 additions & 0 deletions src/Listeners/NotificationSendingListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace CustomD\LaravelHelpers\Listeners;

use Illuminate\Notifications\Events\NotificationSending;

class NotificationSendingListener
{
public function handle(NotificationSending $event)
{
if (method_exists($event->notification, 'blockSend')) {
return ! $event->notification->blockSend($event->notifiable);
}

return true;
}
}

0 comments on commit 37321e6

Please sign in to comment.