laravel-fcm maintained by yamad
yamad/laravel-fcm
Firebase Cloud Messaging API (V1) client for Laravel 10-13.
Uses google/auth for OAuth 2.0 and Guzzle for HTTP.
Requirements
- PHP ^8.1
- Laravel ^10|^11|^12|^13
Installation
composer require yamad/laravel-fcm
The service provider and Fcm facade alias are discovered automatically.
Publish the config file:
php artisan vendor:publish --tag=fcm-config
# .env
FCM_PROJECT_ID=your-firebase-project-id
FCM_CREDENTIALS=/absolute/path/to/service-account.json # optional
FCM_CREDENTIALS を未設定にすると Application Default Credentials(GOOGLE_APPLICATION_CREDENTIALS 環境変数や GCE/GKE のメタデータ)にフォールバックします。
Usage
use YamaD\LaravelFcm\Facades\Fcm;
// Send to a device registration token
$response = Fcm::send([
'token' => $deviceToken,
'notification' => [
'title' => 'Hello',
'body' => 'World',
],
]);
echo $response['name']; // projects/{project_id}/messages/{message_id}
// Send to a topic
Fcm::send([
'topic' => 'news',
'notification' => ['title' => 'Breaking news'],
]);
// Dry run (validation only, no delivery)
Fcm::send($message, validateOnly: true);
DI injection also works:
use YamaD\LaravelFcm\FcmClient;
public function handle(FcmClient $fcm)
{
$fcm->send([...]);
}
The $message payload is passed to the FCM v1 API as-is.
See the official reference for available fields.
Errors: HTTP 4xx/5xx throws GuzzleHttp\Exception\RequestException (the response is attached).
Testing
composer install
vendor/bin/phpunit