PHP SDK for Firebase Cloud Messaging from Google, supporting the HTTP V1 API.
See the official Firebase docs: https://firebase.google.com/docs/cloud-messaging
- PHP >= 7.4
- A Firebase service account JSON file (how to generate one)
Install via Composer:
composer require redjanym/php-firebase-cloud-messagingOr add this to your composer.json and run composer update:
"require": {
"redjanym/php-firebase-cloud-messaging": "2.*"
}use RedjanYm\FCM\Client;
use RedjanYm\FCM\Notification;
use RedjanYm\FCM\Recipient\Device;
$serviceAccountPath = '/path/to/service-account.json';
$client = new Client($serviceAccountPath);
$recipient = new Device('your-device-token');
$notification = new Notification($recipient, 'Title', 'Body', ['key' => 'value']);
$response = $client->send($notification);use RedjanYm\FCM\Client;
use RedjanYm\FCM\Notification;
use RedjanYm\FCM\Recipient\Topic;
$serviceAccountPath = '/path/to/service-account.json';
$client = new Client($serviceAccountPath);
$recipient = new Topic('news');
$notification = new Notification($recipient, 'Title', 'Body', ['key' => 'value']);
$response = $client->send($notification);Clients subscribe to topics from the client app. See the Firebase topic documentation for details on managing topic subscriptions.
The Notification object exposes public properties for platform-specific configuration:
$notification = new Notification($recipient, 'Title', 'Body');
// Android
$notification->androidPriority = 'high';
$notification->androidChannelId = 'my_channel';
$notification->icon = 'ic_notification';
$notification->color = '#FF0000';
// APNs (iOS)
$notification->apnsPriority = '10';
$notification->badge = 5;
$notification->sound = 'default';
$notification->contentAvailable = true;
// General
$notification->image = 'https://example.com/image.png';
$notification->ttl = '3600s';
$notification->clickAction = 'OPEN_ACTIVITY';
$notification->analyticsLabel = 'campaign_123';
// Additional platform-specific settings via extra arrays
$notification->extraNotificationSettings = ['tag' => 'my-tag'];
$notification->extraAPNSHeadersSettings = ['apns-collapse-id' => 'campaign'];
$notification->webPushHeadersSettings = ['Urgency' => 'high'];Install dev dependencies and run the test suite with PHPUnit:
composer install
vendor/bin/phpunitTo run a specific test file:
vendor/bin/phpunit tests/NotificationTest.phpTo run a specific test method:
vendor/bin/phpunit --filter testJsonSerializeWithTopicV2 of this package introduces breaking changes due to the migration from the legacy FCM API to the HTTP V1 API. The new structure is still simple and very similar to the previous one.
The send() method returns a PSR-7 ResponseInterface. Responses follow the standard FCM specifications: