Send Push Notifications From Laravel to a Mobile App With Firebase (FCM)
When an order ships or a payment fails, your mobile app should hear about it even if it is closed. Push notifications do that, and for Android and iOS the usual route is Firebase Cloud Messaging (FCM). This guide shows how a Laravel backend stores device tokens and sends a push through a queued job.
What this article is, and is not
This covers the Laravel side only. Setting up Firebase in the mobile app itself, and the Apple push certificate steps, are separate. Package details change between versions, so check the documentation of the package you install. The example uses kreait/laravel-firebase, a widely used community package.
How it fits together
- The app asks the user for permission and gets a device token from Firebase.
- The app sends that token to your API.
- When something happens, Laravel sends a message to that token through Firebase.
- Firebase delivers it to the phone.
1. Store device tokens
Schema::create('device_tokens', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('token')->unique();
$table->string('platform', 10); // android or ios
$table->timestamps();
});
Route::middleware('auth:sanctum')->post('/v1/device-tokens', function (Request $request) {
$data = $request->validate(['token' => 'required|string', 'platform' => 'required|in:android,ios']);
$request->user()->deviceTokens()->updateOrCreate(['token' => $data['token']], ['platform' => $data['platform']]);
return response()->noContent();
});
A user can have several devices, and tokens change, so the app should send its token at every launch.
2. Install and configure the package
composer require kreait/laravel-firebase
Create a service account key in your Firebase project, store the JSON file outside the public folder, and point to it in .env (see the package documentation for the exact variable name). Never commit that file to git.
3. Send from a queued job
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Laravel\Firebase\Facades\Firebase;
class SendPush implements ShouldQueue
{
use Queueable;
public $tries = 3;
public function __construct(public int $userId, public string $title, public string $body, public array $data = []) {}
public function handle(): void
{
$tokens = DeviceToken::where('user_id', $this->userId)->pluck('token')->all();
if (! $tokens) return;
$message = CloudMessage::new()
->withNotification(Notification::create($this->title, $this->body))
->withData($this->data);
$report = Firebase::messaging()->sendMulticast($message, $tokens);
// Remove tokens that Firebase says are no longer valid
foreach ($report->invalidTokens() as $bad) {
DeviceToken::where('token', $bad)->delete();
}
}
}
Dispatch it from the place the event happens: SendPush::dispatch($order->user_id, 'Order shipped', "Order {$order->number} is on its way", ['order_id' => $order->id]);
4. Keep data small and useful
Push messages have a small size limit. Send an order id in the data, and let the app fetch details from your API when the user taps the notification. Do not put private information in the notification text, because it appears on the lock screen.
Common mistakes
- Sending the push inside the web request. Use a queue so a slow Firebase call cannot slow your API.
- Never deleting invalid tokens, so every send wastes calls on dead devices.
- Committing the Firebase service account file to git.
- Sending sensitive details in the notification text.
Summary
Store one row per device token, send through a queued job, remove invalid tokens after each send, and keep the notification itself short and non-sensitive.