Laravel Packages

Send Notifications in Laravel with Firebase Cloud Messaging and Notifire

Published
Send Notifications in Laravel with Firebase Cloud Messaging and Notifire image

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that enables you to send messages reliably. If you need to use FCM with Laravel, Abdelrazek Kandil has created Notifire, a package for sending FCM notifications with support for Laravel's notification system.

Features

  • Easy integration with Laravel's notification system
  • Support for both simple and complex FCM messages
  • Fluent interface for building notifications
  • Automatic logging of notification delivery status
  • Database migration for storing FCM tokens
  • Configurable default settings

Note: Ensure you already have a Firebase account and project configured, as you will need your Firebase Project ID from the Firebase Console as well as your Firebase service account credentials, which should be stored in a /storage/firebase.json file.

You can install this package using Composer, publish the package files, and run the migrations:

composer require devkandil/notifire
php artisan vendor:publish --provider="DevKandil\NotiFire\FcmServiceProvider"
php artisan migrate

There are also some changes you will need to make to your User model, for example, adding the fcm_token field to your User model's $fillable array.

The package comes with an API endpoint /fcm/token in the built-in FcmController. The endpoint is automatically registered with the Laravel Sanctum authentication middleware so ensure Sanctum is already installed and your user is authenticated before making a request to the endpoint.

You can also add the HasFcm trait to your User model to easily manage FCM tokens. For example:

$user->getFcmToken();
$user->updateFcmToken($token);

Example

use DevKandil\NotiFire\Facades\Fcm;
 
$user = Auth::user();
$fcmToken = $user->getFcmToken();
 
$success = Fcm::withTitle('Laracon US Recap')
->withBody('Check out everything that was announced at Laracon US')
->withImage('https://example.com/image.jpg')
->withIcon("notification_icon")
->withColor("#FF5733")
->withSound('default')
->sendNotification($fcmToken);

Using the Laravel Notification system, you could create a new Notification class like the following:

namespace App\Notifications;
 
use DevKandil\NotiFire\Enums\MessagePriority;
use DevKandil\NotiFire\FcmMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
 
class NewAnnouncement extends Notification
{
use Queueable;
 
public function via(object $notifiable): array
{
return ['fcm'];
}
 
/**
* Get the FCM representation of the notification.
*/
public function toFcm(object $notifiable): FcmMessage
{
return FcmMessage::create('Laravel AI Boost', 'Laravel AI Boost is coming soon!')
->image('https://example.com/image.jpg')
->sound('default')
->clickAction('OPEN_ACTIVITY')
->icon('notification_icon')
->color('#FF5733')
->priority(MessagePriority::HIGH)
->data([
'timestamp' => now()->toIso8601String(),
]);
}
}

And then trigger the notification with:

use \App\Notifications;
 
$user->notify(new NewAnnouncement());

Learn more about this package and view the source code on GitHub.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
Laravel Auditor Audits Your App With Your Own AI Agent image

Laravel Auditor Audits Your App With Your Own AI Agent

Read article
A simple form builder that stays out of your way image

A simple form builder that stays out of your way

Read article
Laravel AI: Trace Agent Runs With Lifecycle Events image

Laravel AI: Trace Agent Runs With Lifecycle Events

Read article
Laravel AI: Get Raw HTTP Responses and Rate Limits image

Laravel AI: Get Raw HTTP Responses and Rate Limits

Read article
Agent Run Observability in Laravel AI SDK 0.11 image

Agent Run Observability in Laravel AI SDK 0.11

Read article
Debounced Queued Event Listeners in Laravel image

Debounced Queued Event Listeners in Laravel

Read article