Sending Firebase Push Notifications in Laravel becomes simple and maintainable when you avoid low-level APIs and follow a structured approach. The following guide shows an integration of Firebase Cloud Messaging into your Laravel application through a clean, scalable way by implementing a reusable service class meeting real production needs.
In this tutorial, you will learn how Firebase push notifications work in Laravel from start to finish. The walkthrough covers creating a Firebase project, configuring the API key, installing the Google Auth package, and building a dedicated notification service. In addition, you will find examples of practical implementations for sending single and bulk push notifications so your Laravel app can reliably communicate with FCM at scale. By the end, you will have a robust and reusable setup to send Firebase Push Notifications in Laravel with confidence, clarity, and long-term maintainability.
What Are Push Notifications and How Firebase FCM Fits In
Push notifications are small, real‑time messages appearing on a user’s device even if the app or browser is not open. You can use them to send order updates, booking reminders, news alerts, or promotional campaigns.
Firebase Cloud Messaging (FCM) is a free service from Google that can send and deliver these push notifications for Android and iOS as well as for the web. Laravel does not send push notifications by itself, but it can call FCM’s HTTP API and trigger messages based on events in your application.
Firebase Push Notifications in Laravel are important when using apps to supply timely notifications on the status of an order shipped, reminding you of a reservation, sending current breaking news events, or even nudging you for missing a workout.
Why Use Firebase Push Notifications with Laravel?
Let’s take a look at reasons developers choose Firebase push notification integration in Laravel:
- Free and reliable: FCM has a generous free tier and global infrastructure, so your messages reach users quickly and reliably.
- Cross‑platform support: You can send to Android, iOS and web from the same Laravel code, instead of managing different services.
- Scales with your product: Whether you have hundreds or millions of devices, FCM handles the delivery layer while Laravel focuses on business logic.
- Rich payloads: You can send titles, bodies, images, and custom data fields that help your app decide what to display or which screen to open.
- Targeted messaging: FCM supports topics and different device tokens, so you can send offers only to buyers, or alerts to users in a specific region.
Creating Firebase Project and FCM API Key
Before you send Firebase Cloud Messaging from Laravel, you need a Firebase project and credentials.
Create a Firebase project
Go to the Firebase console in your browser and login to your account which you want to use for this project. Create or select project and follow the wizard with providing basic details like name of project. Google will generate a unique Project ID automatically store it somewhere. It will take sometime to finalize your project so wait until Firebase finishes creating resources.
Enable Firebase Cloud Messaging and get credentials
- In your Firebase project, open Project settings.
- Go to the Cloud Messaging tab.
- Scroll to the Firebase Cloud Messaging API (V1) section.
- Look for the Service accounts link and open it.
- Click Generate new private key to download a JSON file.
This JSON file is your service account key. It lets your Laravel app authenticate with Google and send notifications through FCM. Store it into root directory and add it into gitignore file.
Remember the absolute path to this file. You will add it to your .env file soon.
Install Google Auth Package and Configure Laravel
To call FCM’s HTTP v1 API securely, Laravel needs to obtain an access token from Google using the service account JSON. We will use the google/auth package for that.
To install this composer package, we will use composer install command into project’s root directory:
composer require google/auth
Next open your .env file and add:
FIREBASE_PROJECT_ID=your-firebase-project-id
FIREBASE_CREDENTIALS=firebase-credentials.json
The project ID is created from firebase while credentials path is our root directory. You need to rename json file name to match this.
Next for better code standard, we will move this environment value into our configuration. Open config/services.php and add a Firebase entry:
'firebase' => [
'project_id' => env('FIREBASE_PROJECT_ID'),
'credentials' => env('FIREBASE_CREDENTIALS'),
],
Create a Laravel Firebase Push Notification Service Class
To keep your code organised and reusable, you will create a dedicated service class. Other parts of your app will call this service instead of talking to FCM directly.
Let’s create new service class called app/Services/FirebasePushService.php. This service will hold together all the logic and operation for firebase push notification in your Laravel application.
<?php
namespace App\Services;
use Google\Auth\Credentials\ServiceAccountCredentials;
use Illuminate\Support\Facades\Http;
class FirebasePushService
{
protected $projectId;
protected $accessToken;
protected $endpoint;
public function __construct()
{
$this->projectId = config('services.firebase.project_id');
$jsonKeyPath = config('services.firebase.credentials');
$jsonKey = json_decode(file_get_contents($jsonKeyPath), true);
$credentials = new ServiceAccountCredentials(
'https://www.googleapis.com/auth/firebase.messaging',
$jsonKey
);
$token = $credentials->fetchAuthToken();
$this->accessToken = $token['access_token'];
$this->endpoint = 'https://fcm.googleapis.com/v1/projects/' . $this->projectId . '/messages:send';
}
public function sendNotification(string $deviceToken, string $title, string $body, array $data = []): array
{
$payload = [
'message' => [
'token' => $deviceToken,
'notification' => [
'title' => $title,
'body' => $body,
],
'data' => $data,
],
];
$response = Http::withToken($this->accessToken)
->post($this->endpoint, $payload);
return [
'success' => $response->successful(),
'status' => $response->status(),
'body' => $response->json(),
];
}
public function sendNotifications(array $deviceTokens, string $title, string $body, array $data = []): array
{
$responses = [];
foreach ($deviceTokens as $token) {
$responses[$token] = $this->sendNotification($token, $title, $body, $data);
}
return $responses;
}
}
In service class, uses our credentials and authenticate to google. Once authenticated it will have token which used for sending notification. It has two methods one for sending notification to single user while another one will be used for sending notification to multiple users through loop.
Send Firebase Cloud Messaging From Laravel
Now that your service class is ready, you can plug it into a real workflow. This section shows a full example of how to send Firebase cloud messaging from Laravel in a practical way. Your mobile app (Android or iOS) or web app will use the Firebase SDK to obtain a device token. After login or app start, the client should send this token to your Laravel backend via an API call.
In real-life example, tokens are stored to database with single/multiple tokens for user. With flow like when user logged into real device it will send token to our Laravel back end and we will store it to database. However, for demonstration we will use simple form to send notification.
Let us create a controller that uses the FirebasePushService class. Open terminal and enter below command to generate controller:
php artisan make:controller PushNotificationController
Open app/Http/Controllers/PushNotificationController.php and update it:
<?php
namespace App\Http\Controllers;
use App\Services\FirebasePushService;
use Illuminate\Http\Request;
class PushNotificationController extends Controller
{
protected $firebasePush;
public function __construct(FirebasePushService $firebasePush)
{
$this->firebasePush = $firebasePush;
}
public function sendTest(Request $request)
{
$request->validate([
'token' => 'required|string',
]);
$response = $this->firebasePush->sendToDevice(
$request->input('token'),
'Hello from Laravel',
'This is a test push notification from your Laravel app.',
[
'screen' => 'home',
'clicked_at' => now()->toDateTimeString(),
]
);
return response()->json($response);
}
}
Next, add a route for this endpoint. If you use routes/api.php, add:
Route::post('/send-test-notification', [PushNotificationController::class, 'sendTest']);
To test this open postman or use mobile device to call this API. Here you need to pass actual token for any device to check this functionality.
Now you will use the sendToDevices method for a batch notification. This is useful for campaigns like flash sales, promo codes, or critical alerts. Add another method to your PushNotificationController:
public function sendPromotion(Request $request)
{
$request->validate([
'tokens' => 'required|array',
'tokens.*' => 'string',
]);
$tokens = $request->input('tokens');
$responses = $this->firebasePush->sendToDevices(
$tokens,
'Weekend Flash Sale',
'Everything is 30 percent off until Sunday night. Tap to see the best deals.',
[
'screen' => 'offers',
'campaign' => 'weekend_flash_sale',
]
);
return response()->json($responses);
}
Add the corresponding route:
Route::post('/send-promotion', [PushNotificationController::class, 'sendPromotion']);
You can test this endpoint using postman with passing tokens as array into your request. In a real application, you would not send the tokens from the client for a promotion like this. Instead, you would pull tokens from your database, for example: fetching tokens from user table with additional check for signup for notification or purchased something in last 30 days, etc…
Summary
The above tutorial has shown the pretty clear and straightforward way of sending Firebase Push Notifications in Laravel using Firebase Cloud Messaging. You have set up a Firebase project, configured credentials, installed the required package, and built a reusable service that handles single and batch notifications with real use cases, such as order updates and promotions.
With this base in place, you can easily scale your Laravel Firebase push notification setup by adding queues, logging FCM responses, or targeting users using topics. What would otherwise have been a simple tutorial now escalates to a reliable notification system that could scale when the application does.
