PHPackages                             plokko/php-fcm-v1 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. plokko/php-fcm-v1

ActiveLibrary[API Development](/categories/api)

plokko/php-fcm-v1
=================

Php implementation of FCM v1 Api

14516[1 issues](https://github.com/plokko/php-fcm-v1/issues)PHP

Since Mar 8Pushed 8y ago4 watchersCompare

[ Source](https://github.com/plokko/php-fcm-v1)[ Packagist](https://packagist.org/packages/plokko/php-fcm-v1)[ RSS](/packages/plokko-php-fcm-v1/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependenciesVersions (1)Used By (0)

php-fpm-v1
==========

[](#php-fpm-v1)

FCM App server protocol v1 php implementation

**NOTE: this project is now part of [plokko/firebase-php](https://github.com/plokko/firebase-php) and may be deprecated**

Installation:
-------------

[](#installation)

Install with composer via

`$composer require plokko\php-fcm-v1`

Usage:
------

[](#usage)

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
$message->notification
    ->setTitle('My notification title')
    ->setBody('My notification body....');

```

The `Data` payload

```
$message->data->fill([
    'a'=>1,
    'b'=>'2',
]);
$message->data->set('x','value');
$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'));
//Or
$message->setTarget(new Topic('your-fcm-topic'));
//Or
$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 [https://firebase.google.com/docs/admin/setup#initialize\_the\_sdk](https://firebase.google.com/docs/admin/setup#initialize_the_sdk) ).

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->setHttpClient($myClient);
$request->validate_only = true;
//or
$request->validateOnly(true);
```

To send the `Message` use the `send` method:

```
$message->send($request);
```

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.
```php
$request->validateOnly(false);
$message->validate($request);//will be a validate_only request anyway
```

### Full example:

[](#full-example)

```
