There is no license information available for the latest version (dev-master) of this package.

Php implementation of FCM v1 Api

dev-master 2018-03-08 15:03 UTC

This package is auto-updated.

Last update: 2021-01-17 17:54:36 UTC


FCM App server protocol v1 php implementation

NOTE: this project is now part of plokko/firebase-php and may be deprecated


Install with composer via

$composer require plokko\php-fcm-v1


The FCM message is build using the Message class

$message = new Message();

This class contains all the API V1 objects like Notification

//Set the message notification
    ->setTitle('My notification title')
    ->setBody('My notification body....');

The Data payload

$messsage->data->y='Same as above';

And system-specific configuration like AndroidConfig, WebpushConfig and ApnsConfig.

The message require that the Target parameter is specified with either a single device (Token) Topic or Condition

$message->setTarget(new Token('your-fcm-device-token'));
$message->setTarget(new Topic('your-fcm-topic'));
$message->setTarget(new Condition('your-fcm-conditions'));

If this value is not set the message will throw an error before submitting.

Before submitting we need to downloaded a JSON file with your service account credentials (see ).

This file is needed to initialize the class ServiceAccount that's used to generate an OAuth2 token for the FCM request.

// Prepare service account
$sa = new ServiceAccount('path/to/your/firebase-credentials.json');

The ServiceAccount will not be used directly to submit the message but to build the Request:

$request = new Request($sa);

For testing purpuses you can also set the validate_only parameter to test the message in Firebase without submitting it to the device; the Request's http client can also be overriddent with a custom GuzzleHttp client.

//Custom http client
$myClient = new Client(['debug'=>true]);

$request->validate_only = true; 

To send the Message use the send method:


If the function will not throw a FcmError, BadRequest or a generic GuzzleException the message has been successfully sent and, if it's not a validate_only request, the message name will be populated.

//after submitting:
echo 'my message name:'.$message->name; 

If you want to use the validate_only without modifying the request the `validate` method will force the validate_only flag on the request.
$message->validate($request);//will be a validate_only request anyway

Full example:

require dirname(__DIR__) . '/vendor/autoload.php';

use GuzzleHttp\Client;
use Plokko\PhpFcmV1\Message;
use Plokko\PhpFcmV1\Request;
use Plokko\PhpFcmV1\ServiceAccount;
use Plokko\PhpFcmV1\Targets\Token;

// Your Firebase credential file path
$firebase_secrets_file = dirname(__DIR__).'/.firebase-credentials.json';

// Prepare service account
$sa = new ServiceAccount($firebase_secrets_file);

$message = new Message();

// Add a data payload,NB: everything will be converted as a string:string
    'a'=>1,//Will be converted to '1'
    'n'=>null,//Will be converted to ''

//Add a notification
    ->setTitle('My notification title')
    ->setBody('My notification body....');

//Set the message destination
$message->setTarget(new Token('your-device-token'));

//Custom http client (optional)
$myClient = new Client(['debug'=>true]);

//Prepare the request
$request = new Request($sa,true,$myClient);



If you get a 403 error "Firebase Cloud Messaging API has not been used in project <project_name> before..." that's because the new v1 API is not enabled automatically (even if you genereated the credentials from Firebase Console and the legacy Api works).

You need to enable it from this page: