Laravel Notification - Getting API Response from Custom Notification Channel

Updated: Feb 05, 2025

Laravel Notification - Getting API Response from Custom Notification Channel

In Laravel, notifications are a way to send various types of messages to users. Laravel comes with several built-in notification channels, such as mail, database, broadcast, and seomail. However, you can also create custom notification channels to send notifications via third-party APIs or other custom methods.

To get an API response from a custom notification channel, you need to implement the ShouldQueue and SendMessage interfaces in your custom channel class. Here's a step-by-step guide on how to create a custom notification channel and get an API response:

  1. Create a new custom notification channel class in the app/Notifications directory. Let's call it ApiNotificationChannel.
namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queueable;
use Illuminate\Notifications\Channels\DatabaseChannel;
use Illuminate\Notifications\Channels\DispatchableChannelContract;
use Illuminate\Support\Facades\Http;

class ApiNotificationChannel implements Queueable, DispatchableChannelContract
{
    // ...
}
  1. Implement the ShouldQueue and DispatchableChannelContract interfaces. This will allow your notification channel to be queued and dispatched using Laravel's queueing system.
namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queueable;
use Illuminate\Notifications\Channels\DatabaseChannel;
use Illuminate\Notifications\Channels\DispatchableChannelContract;
use Illuminate\Support\Facades\Http;

class ApiNotificationChannel implements Queueable, DispatchableChannelContract
{
    // ...

    public function shouldQueue($notifiable)
    {
        return true;
    }

    public function dispatch($notification)
    {
        return app(static::class)->send($notification);
    }
}
  1. Implement the sendMessage method, which will handle sending the notification via the API.
namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queueable;
use Illuminate\Notifications\Channels\DatabaseChannel;
use Illuminate\Notifications\Channels\DispatchableChannelContract;
use Illuminate\Support\Facades\Http;

class ApiNotificationChannel implements Queueable, DispatchableChannelContract
{
    // ...

    public function send($notification)
    {
        $response = Http::withHeaders([
            'Authorization' => 'Bearer your-api-token',
        ])->post('https://api.example.com/notifications', [
            'user_id' => $notification->user_id,
            'message' => $notification->message,
        ]);

        if ($response->status() == 200) {
            return true;
        }

        throw new \Exception('Failed to send notification via API.');
    }
}

Replace 'Authorization' => 'Bearer your-api-token' with the actual API token or authentication headers required by the third-party API.

  1. Register your custom notification channel in the config/app.php file.
'notifications' => [
    // ...

    'App\Notifications\ApiNotificationChannel' => [
        'driver' => 'api',
    ],
],
  1. Use your custom notification channel in your notification class.
namespace App\Notifications;

use Illuminate\Notifications\Notification;

class NewUserRegistered extends Notification
{
    protected $user;

    public function __construct($user)
    {
        $this->user = $user;
    }

    public function via($notifiable)
    {
        return ['api'];
    }

    public function toArray($notifiable)
    {
        return [
            'message' => 'A new user has registered.',
            'user_id' => $this->user->id,
        ];
    }
}
  1. Finally, send the notification using your custom notification channel.
use App\User;
use App\Notifications\NewUserRegistered;

$user = new User();
$user->save();

$user->notify(new NewUserRegistered($user));

This example demonstrates how to create a custom notification channel in Laravel and get an API response when sending a notification. You can modify the code to fit your specific use case and third-party API requirements.