Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What is the process to register a custom notifiable class for notifications


What is the process to register a custom notifiable class for notifications


To register a custom notifiable class for notifications in Laravel, you can follow these steps:

1. Create a Notifiable Class:
Create a new class that implements the `Illuminate\Contracts\Notifications\Notifiable` interface. This class will represent the entity that can receive notifications.

php
   namespace App\Models;

   use Illuminate\Contracts\Notifications\Notifiable;
   use Illuminate\Database\Eloquent\Model;

   class User extends Model implements Notifiable
   {
       use \Illuminate\Notifications\Notifiable;

       // Your user model properties and methods
   }
   

In this example, the `User` model implements the `Notifiable` interface and uses the `Illuminate\Notifications\Notifiable` trait.

2. Register the Notifiable Class:
In your `app/Providers/EventServiceProvider.php` file, add a `$listen` array that maps the `Illuminate\Notifications\Events\NotificationSending` event to a custom listener that will register your notifiable class.

php
   namespace App\Providers;

   use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
   use Illuminate\Notifications\Events\NotificationSending;
   use App\Listeners\RegisterNotifiableClass;

   class EventServiceProvider extends ServiceProvider
   {
       protected $listen = [
           NotificationSending::class => [
               RegisterNotifiableClass::class,
           ],
       ];

       // Other event listeners...
   }
   

3. Create the Listener:
Create a new listener class called `RegisterNotifiableClass` in the `app/Listeners` directory. This listener will register your custom notifiable class with the notification system.

php
   namespace App\Listeners;

   use Illuminate\Notifications\Events\NotificationSending;
   use App\Models\User;

   class RegisterNotifiableClass
   {
       public function handle(NotificationSending $event)
       {
           $notifiable = $event->notifiable;

           if ($notifiable instanceof User) {
               // Register the User class as a notifiable class
               $event->channels->add('user');
           }
       }
   }
   

In this example, the `handle` method checks if the `$notifiable` instance is an instance of the `User` class. If so, it registers the `user` channel for that notifiable class.

4. Use the Notifiable Class:
Now, when you send a notification, you can use the registered notifiable class to target the notification:

php
   $user = User::find(1);
   $user->notify(new YourNotification());
   

The notification will be delivered to the registered `user` channel for the `User` class.

By following these steps, you can register a custom notifiable class for your Laravel application's notifications. This allows you to use your own custom models or entities as the recipients of your notifications, providing more flexibility and control over the notification system.

Citations:
[1] https://studysection.com/blog/custom-notification-in-laravel/
[2] https://laravel.com/docs/11.x/notifications
[3] https://laravel.com/docs/5.4/notifications
[4] https://dev.to/grafstorm/writing-a-simple-custom-laravel-notification-channel-4f33
[5] https://moedayraki.github.io/blog/posts/laravel-notification-level