Laravel API -Send Notifications to android and iOS client apps with Firebase FCM
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Laravel API: Sending Real-Time Notifications to Android and iOS with Firebase FCM
Building robust, real-time communication between a backend (Laravel API) and mobile clients (Android/iOS) is a common challenge in modern application development. When you need to push critical events—like "New Offer Created" or "New Stock Available"—to all active users simultaneously, relying on traditional REST polling or deprecated methods like raw curl requests becomes inefficient and brittle.
The solution lies in leveraging Firebase Cloud Messaging (FCM). While FCM is designed for direct device-to-server communication via tokens, your Laravel backend acts as the secure intermediary, using the Firebase Admin SDK to push messages efficiently. This approach is far more scalable, reliable, and adheres to modern API design principles, aligning perfectly with best practices seen in frameworks like those promoted by laravelcompany.com.
The Architecture: Laravel as the FCM Gateway
The core problem is not how to send data, but how to securely authenticate and structure the request. Since you are a server-side application (Laravel), the correct flow involves your application initiating the push through Google's infrastructure, rather than trying to directly manipulate client tokens via external HTTP calls that might be deprecated or insecure.
Here is the recommended architecture:
- Event Trigger: An event occurs in your database (e.g., a new offer is created).
- Laravel Processing: A Laravel job or controller handles this event.
- FCM Interaction: Laravel uses the official Firebase Admin SDK to communicate with the FCM service, authenticated via a Service Account key.
- Notification Delivery: FCM securely routes the message to the relevant Android and iOS devices.
Step-by-Step Implementation in Laravel
To implement this effectively, you need the Firebase Admin SDK for PHP. This allows your Laravel application to assume the role of an authorized server capable of sending messages on behalf of your application.
1. Setup Authentication
You must configure your Laravel project to use a Service Account key file (usually a JSON file downloaded from the Firebase console) to authenticate with the FCM service.
2. Utilizing the Firebase Admin SDK
Instead of manually crafting complex HTTP requests, you interact directly with the SDK methods within your Laravel services or controllers.
Here is a conceptual example demonstrating how you might structure the notification sending logic in a dedicated service class:
<?php
namespace App\Services;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging\CloudMessage as FcmCloudMessage;
use Kreait\Firebase\Messaging\WebPushToken; // Or use the correct token structure
class NotificationService
{
protected $messaging;
public function __construct()
{
// Initialize the Firebase Messaging client using your service account credentials
$this->messaging = \Kreait\Firebase\App::instance()->messaging();
}
/**
* Sends a notification to a specific device token.
*
* @param string $fcmToken The target device registration token.
* @param string $title The title of the notification.
* @param string $body The content of the notification.
* @return bool
*/
public function sendNotification(string $fcmToken, string $title, string $body): bool
{
try {
$message = CloudMessage::withTarget('token', $fcmToken)
->withNotification(Notification::create($title, $body));
$this->messaging->send($message);
return true;
} catch (\Exception $e) {
// Log the error for debugging purposes
\Log::error("FCM Notification Failed: " . $e->getMessage());
return false;
}
}
}
3. Triggering the Notification
When a new offer is created, your Eloquent model triggers this service:
// In your OfferController or Job
use App\Services\NotificationService;
class OfferController extends Controller
{
public function create(Request $request)
{
// ... logic to save the new offer to the database ...
$fcmToken = $user->fcm_token; // Retrieve the user's stored token
$title = "New Offer Created!";
$body = "A new offer has just been posted for you.";
$notificationService = app(NotificationService::class);
// Send the notification via the reliable FCM channel
$success = $notificationService->sendNotification($fcmToken, $title, $body);
if ($success) {
return response()->json(['message' => 'Offer created and notification sent successfully']);
} else {
return response()->json(['error' => 'Failed to send notification'], 500);
}
}
}
Conclusion: Why This Method Wins
By integrating the Firebase Admin SDK into your Laravel backend, you achieve several critical benefits:
- Reliability: You are using Google’s official, highly reliable messaging infrastructure instead of custom HTTP wrappers.
- Security: Authentication is handled securely via Service Account credentials, keeping sensitive keys off the client side.
- Scalability: Laravel can handle massive event queues, and FCM handles the distribution load efficiently, making this solution scalable for millions of users.
For any serious application built on Laravel, leveraging official SDKs like those provided by Firebase is the definitive path forward for real-time communication. Focus on clean service separation, as demonstrated above, to ensure your code remains maintainable and robust, much like well-designed APIs seen in laravelcompany.com.