Custom Notification Channel in Laravel
Do you want to send push notifications to your users? Laravel Notification Channels makes it easy to send notifications using a variety of services, including SMS, email, and Slack. In this article, we will discuss how to send push notifications to your users using Firebase Notification in Laravel.
Prerequisites
Install laravel-notification-channels/fcm package and configure it, you are free to use any other FCM package as well.
composer require kreait/laravel-firebase
Create a New Notification Channel Class
it should look like this:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notification;
class FirebaseChannel
{
public Messaging $messaging;
public function __construct()
{
$this->messaging = $this->connect();
}
/**
* Send the given notification.
* @throws MessagingException
* @throws FirebaseException
*/
public function send(object $notifiable, Notification $notification): void
{
$message = $notification->toFirebase($notifiable);
/** @var User $notifiable */
$deviceToken = $notifiable->device_token;
if (!isset($deviceToken)) {
throw new RuntimeException('device_token not set for this user: '. $notifiable->full_name);
}
$this->messaging->send(
CloudMessage::withTarget('token', $deviceToken)->withNotification($message)
);
}
public function connect(): Messaging
{
$firebase = (new Factory)->withServiceAccount(base_path(config('services.firebase.credentials')));
return $firebase->createMessaging();
}
}
Explanation
The FirebaseChannel
class is defined within the App\Notifications
namespace.
The class has a Messaging property, which is a dependency injection of the FCM Messaging class.
The class constructor initializes the Messaging
property by calling the connect()
method.
The send()
method is used to send notifications. It accepts a $notifiable
object and a $notification
object.
The toFirebase()
method is called on the $notification
object to generate a message to be sent.
The device_token
property is retrieved from the $notifiable
object and is used to identify the device to send the notification to.
A CloudMessage
object is created with the target set to the device token and the notification message set to the message generated by the $notification->toFirebase()
method.
The Messaging object's send()
method is called, passing in the CloudMessage
object, to send the notification to the specified device.
The connect()
method creates a Factory object with the Firebase service account credentials file and returns a Messaging
object.
Create a New Notification Class
that will use our new custom channel
php artisan make:notification BroadcastNotification
Edit the BroadcastNotification class
it should look like this:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
class BroadcastNotification extends Notification implements ShouldQueue
{
use Queueable; //<- if you want to queue the notification
public function __construct(
private readonly string $title,
private readonly string $message
)
{
}
public function via(object $notifiable): string
{
return FirebaseChannel::class; // <- this is the channel we created
}
public function toFirebase($notifiable): array // <- this is the method that will be called in the channel to shape the notification
{
return [
'title' => $this->title,
'body' => $this->message,
];
}
}
Send the notification
$user->notify(new BroadcastNotification('title', 'message'));
Conclusion
In this article, we have discussed how to send push notifications to your users using Firebase Notification in Laravel. We have also discussed how to create a custom notification channel in Laravel. I hope you have enjoyed this article and found it useful. If you have any questions or suggestions, please feel free to leave a comment below.