warith / laravel-fcm-httpv1
A Laravel package to send Firebase Cloud Messaging (FCM) notifications across Laravel applications.
Requires
- php: ^7.4|^8.0
- illuminate/support: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0
README
A simple package that helps you send Firebase notifications using HTTP v1 with your Laravel applications. ๐ฏ๐ฌ๐ก
Installation ๐ฆโก๏ธ๐ป
You can install the package via composer: ๐๐ฅ๐ ๏ธ
composer require warith313/laravel-fcm-httpv1 "^0.0.1"
Laravel Setup ๐ ๏ธ๐ฃ๐
Register the service provider: ๐๏ธ๐ข๐ก
// config/app.php 'providers' => [ // ... Warith\Fcm\FcmServiceProvider::class, ],
Optional: add the Facade for easy access: ๐ช๐โจ
// config/app.php 'aliases' => [ // ... 'Fcm' => Warith\Fcm\FcmFacade::class, ],
Publish the config file: ๐๐ฃ๐ผ
php artisan vendor:publish --provider="Warith\Fcm\FcmServiceProvider"
Published file content (config/laravel-fcm.php): ๐๐๐ง
return [ /** * Path to your Firebase service account JSON file */ 'service_account_path' => env('FCM_SERVICE_ACCOUNT_PATH', storage_path('app/service-account.json')), ];
Set the path in your .env: ๐๐๐
FCM_SERVICE_ACCOUNT_PATH=/storage/app/service-account.json
Lumen Setup ๐ง๐กโก๏ธ
Add the service provider in bootstrap/app.php: ๐๏ธ๐ก๐
$app->register(Warith\Fcm\FcmServiceProvider::class);
Copy the config file to config/laravel-fcm.php and configure before registering the provider: ๐โ๏ธ๐
$app->configure('laravel-fcm'); $app->register(Warith\Fcm\FcmServiceProvider::class);
Methods Reference ๐โ๏ธ๐
->to($recipients)โ Send to a single token (string) or array of tokens->toTopic($topic)โ Send to a topic->data($array)โ Custom data payload->notification($array)โ Notification payload->priority('high'|'normal')โ Notification priority->timeToLive(seconds)โ Time to live in seconds (0 to 2419200)->enableResponseLog()โ Log raw Firebase response->send()โ Send the message
Usage Examples ๐ฌ๐ฅ๐ฏ
1. Send only data payload: ๐๐๏ธโก๏ธ
$recipients = [ 'token1...', 'token2...', ]; fcm() ->to($recipients) ->priority('high') ->timeToLive(0) ->data([ 'title' => 'Test FCM', 'body' => 'This is a test FCM', ]) ->send();
2. Send to a topic: ๐ข๐ฐ๐ฏ
$topic = 'news'; fcm() ->toTopic($topic) ->priority('normal') ->timeToLive(0) ->notification([ 'title' => 'Topic FCM', 'body' => 'This is a topic test', ]) ->send();
3. Send only notification payload: ๐๐โจ
fcm() ->to($recipients) ->priority('high') ->timeToLive(0) ->notification([ 'title' => 'Test Notification', 'body' => 'Notification only payload', ]) ->send();
4. Send both data & notification payloads with image ๐ผ๏ธ๐ฑ๐
fcm() ->to($recipients) ->priority('high') ->timeToLive(0) ->data([ 'title' => 'Test FCM', 'body' => 'This is a data payload', 'type' => 'all', 'click_action' => 'FLUTTER_NOTIFICATION_CLICK', 'image' => $image_link ]) ->notification([ 'title' => 'Test FCM', 'body' => 'This is a notification payload', 'image' => $image_link ]) ->send();
Note: When sending images, the package automatically sets
mutable-contentfor APNs, adds image to Android notification, and includes Webpush headers. ๐ท๐ฒ๐จ
Logging ๐๐๐
To see the raw Firebase response: ๐ฅ๏ธ๐โจ
fcm() ->to($recipients) ->enableResponseLog() ->send();
Check logs at storage/logs/laravel.log. ๐๏ธ๐๐ฅ
Notes for HTTP v1 โก๏ธ๐๐ก
tokenmust be a single string per message. For multiple tokens, pass an array to->to($tokens)and the package sends each individually.- APNs overrides (
badge,mutable-content) are automatically added ifimageis provided. - Android overrides (
click_action,image) are automatically handled. - Webpush image headers are automatically added if
imageis present. timeToLivemust be between0and2419200(28 days). ๐ฏ๐ฌ๐
License ๐
This package is open-sourced software licensed under the MIT license.