Download the PHP package mve/fcm-php without Composer
On this page you can find all versions of the php package mve/fcm-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package fcm-php
Firebase Cloud Messaging for PHP
Send Google Firebase Cloud messages with PHP.
API documentation
Preparation
First make sure you have a Firebase project and you have downloaded the private key JSON file. See https://firebase.google.com/docs/admin/setup for more information.
Each call to the The Google API has to be authenticated with an access token that is fetched from the Google API. These access tokens expire after an hour. This library will take care of getting an access token from the Google API and cache it for an hour. However it doesn't implement a caching mechanism itself, but it expects an implementation of the Mve\FcmPhp\Models\CacheInterface
from the caller.
Getting started
Sending messages is done with the Messaging
class:
The arguments:
- An instance of a
Mve\FcmPhp\Models\CacheInterface
implementation as explained earlier. - The path to the Google Firebase private key JSON file.
- An optional
Psr\Log\LoggerInterface
implementation. This is only needed if you want to see the requests and responses logged.
Sending messages to devices
The sendAll
method uses HTTP/2 multiplexing. It's very fast and can be used to send a batch of notifications.
Testing
First make sure the environment variable JSON_FILE
points to your Google Firebase private key JSON file:
export JSON_FILE="/path/to/file.json"
If you want to test one or more valid registration tokens, set then to the TOKENS
environment variable:
export TOKENS="token1, token2"
If you already have an access token that you want to use during the tests, you can set it with:
export ACCESS_TOKEN="my-access-token"
Test getting a valid access token
This will test getting an access token from the Google API.
./vendor/bin/phpunit tests/AccessTokenHandlerTest.php
Test invalid registration tokens
This will test some invalid tokens.
./vendor/bin/phpunit tests/MessagingWithoutEnvTokenTest.php
Test valid registration tokens
This will test one or more valid tokens.
Only works if you have set the TOKENS
environment variable.
./vendor/bin/phpunit tests/MessagingWithEnvTokenTest.php