Push notifications bring users back to your app with timely updates: new messages, order status, reminders or alerts. Firebase Cloud Messaging (FCM) is the most common way to deliver them to Android, iOS and web browsers, and Laravel’s notification system makes sending them clean and testable. This guide shows how to send push notifications from Laravel with Firebase using the current FCM HTTP v1 API, from Firebase setup to sending, client handling and production tips.
Important: Google shut down FCM’s legacy APIs (the “server key” approach) in 2024. If an older tutorial asks for FIREBASE_SERVER_KEY, it will no longer work. Use a service account and the HTTP v1 API as shown below.
How push notifications work with Laravel and FCM
- Your app (web, Android or iOS) asks the user for permission and gets an FCM registration token for that device.
- The app sends the token to your Laravel back end, which stores it against the user.
- When something happens (a new message, an order update), Laravel sends a message to FCM with the token.
- FCM delivers the notification to the device.
Step 1: set up Firebase
- Create a project in the Firebase console.
- Add your apps (Web, Android and/or iOS) to the project.
- For web push, generate a Web Push certificate (VAPID key) under Project settings → Cloud Messaging.
- For iOS, upload your APNs authentication key so FCM can deliver to Apple devices.
- Under Project settings → Service accounts, generate a new private key. This JSON file lets your server authenticate with FCM. Store it outside your web root and never commit it to Git.
Step 2: install the packages
The laravel-notification-channels/fcm package adds an FCM channel to Laravel notifications and uses kreait/laravel-firebase underneath:
composer require laravel-notification-channels/fcm
Point Laravel to your service account file in .env:
FIREBASE_CREDENTIALS=/var/www/secure/firebase-service-account.json
Optionally publish the Firebase config to customise it:
php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config
Step 3: store device tokens
Add a column (or a separate table if users have several devices) and an endpoint the app calls after it receives a token:
Schema::table('users', function (Blueprint $table) {
$table->string('fcm_token')->nullable();
});
Route::post('/device-token', function (Request $request) {
$request->validate(['token' => 'required|string']);
$request->user()->update(['fcm_token' => $request->token]);
return response()->noContent();
})->middleware('auth:sanctum');
Tell Laravel which token to use for the notification channel:
// app/Models/User.php
public function routeNotificationForFcm(): ?string
{
return $this->fcm_token;
}
Free resource
Upgrade and patching checklist
Want this applied to your project? Our engineers will send it with notes for your stack.
Step 4: create the notification
php artisan make:notification NewMessageNotification
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use NotificationChannels\Fcm\FcmChannel;
use NotificationChannels\Fcm\FcmMessage;
use NotificationChannels\Fcm\Resources\Notification as FcmNotification;
class NewMessageNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(private int $messageId, private string $sender) {}
public function via(object $notifiable): array
{
return [FcmChannel::class];
}
public function toFcm(object $notifiable): FcmMessage
{
return (new FcmMessage(notification: new FcmNotification(
title: 'New message',
body: "{$this->sender} sent you a message",
)))->data([
'message_id' => (string) $this->messageId,
'type' => 'chat',
]);
}
}
Implementing ShouldQueue sends notifications through your queue, so web requests stay fast. Data values must be strings.
Step 5: send the notification
$recipient->notify(new NewMessageNotification($message->id, $sender->name));
Check the package’s README for the version you install, as the message-builder API has changed between major versions.
Sending directly with the Firebase SDK
For broadcasts or topic messages you can use kreait/laravel-firebase directly:
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Laravel\Firebase\Facades\Firebase;
$message = CloudMessage::withTarget('topic', 'promotions')
->withNotification(['title' => 'Weekend sale', 'body' => '20% off this weekend'])
->withData(['screen' => 'offers']);
Firebase::messaging()->send($message);
Step 6: receive notifications in a web app
Use the modular Firebase JavaScript SDK to request permission and get a token:
import { initializeApp } from 'firebase/app';
import { getMessaging, getToken, onMessage } from 'firebase/messaging';
const app = initializeApp({ /* your Firebase web config */ });
const messaging = getMessaging(app);
const token = await getToken(messaging, { vapidKey: 'YOUR_PUBLIC_VAPID_KEY' });
await fetch('/device-token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token }),
});
onMessage(messaging, (payload) => {
// Show an in-app toast while the page is open
console.log('Message received', payload);
});
Add a firebase-messaging-sw.js service worker at your site root to handle notifications when the page is in the background, following the Firebase documentation for your SDK version.
Receiving notifications in mobile apps
- Flutter: use the
firebase_messagingplugin to request permission, get the token and handle foreground, background and tapped notifications. - Android (Kotlin): add Firebase Messaging, extend
FirebaseMessagingServiceand handleonNewTokenandonMessageReceived. Android 13+ requires the runtime notification permission. - iOS (Swift): enable push notifications and background modes, upload your APNs key to Firebase and request authorisation from the user.
- React Native: use React Native Firebase messaging.
Production best practices
- Handle invalid tokens: remove tokens that FCM reports as unregistered or invalid.
- Support multiple devices per user with a
device_tokenstable and send to each. - Use queues and retries so spikes in notifications do not slow your app.
- Use topics for broadcasts instead of looping over thousands of tokens.
- Respect users: ask permission at a meaningful moment, let users choose notification types, and avoid sending too many.
- Secure credentials: keep the service account file out of version control and restrict its permissions.
- Measure engagement: track deliveries, opens and conversions.
Frequently asked questions
Does the Firebase server key still work? No. FCM’s legacy APIs were shut down in 2024. Use the HTTP v1 API with a service account.
Which Laravel package should I use for Firebase push notifications? laravel-notification-channels/fcm for Laravel notifications, built on kreait/laravel-firebase, which you can also use directly.
Can I send push notifications to iOS with Firebase? Yes. Upload your APNs key to Firebase and FCM delivers notifications to iOS devices.
How do I send a notification to many users at once? Subscribe devices to a topic and send one message to the topic, or send in batches through a queue.
Do web push notifications work on iPhone? Yes, for web apps added to the home screen on supported iOS versions, using the standard Web Push permission flow.
Need push notifications in your app?
Vibidsoft builds Laravel back ends and mobile apps with notifications, real-time features and integrations. See our Laravel development services and mobile app development, or contact us.
