PHP code example of autoxloo / fcm

1. Go to this page and download the library: Download autoxloo/fcm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

autoxloo / fcm example snippets


composer 

$projectId = 'autoxloo';                                        // id of your project created in firebase console
$serviceAccountFilePath = __DIR__ . '/service_account.json';    // path to your generated private key file for your service account

$fcm = new FirebaseCloudMessaging($projectId, $serviceAccountFilePath);
$response = $fcm->send($message);   // $message is instance of \autoxloo\fcm\message\Message
                                    // $response is instance of \GuzzleHttp\Psr7\Response

// initial data:
$projectId = 'autoxloo';
$serviceAccountFile = __DIR__ . '/service_account.json';
$token = 'some device token';
$name = 'Some name';
$title = 'Some title';
$body = 'Some body';
$data = [
    'some key1' => 'some value1',
    'some key2' => 'some value2',
]; 

// sending push notification:

$target = FCMFacade::createTargetToken($token);     // only target is irebaseCloudMessaging($projectId, $serviceAccountFile);
$response = $fcm->send($message);   // $response is instance of \GuzzleHttp\Psr7\Response

$messageConfig = [
    // => $token,     // or Message::FIELD_TOPIC => $topic or Message::FIELD_CONDITION => $condition

    // not n($title, $body),
    Message::FIELD_ANDROID => FCMFacade::createAndroidConfig([
        AndroidConfig::FIELD_PRIORITY => AndroidConfig::PRIORITY_HIGH
   ]),
];

$message = FCMFacade::createMessage($messageConfig);

$fcm = new FirebaseCloudMessaging($projectId, $serviceAccountFile);
$response = $fcm->send($message);   // $response is instance of \GuzzleHttp\Psr7\Response

$targetToken = FCMFacade::createTargetToken('some token');
$targetTopic = FCMFacade::createTargetTopic('some topic');
$targetCondition = FCMFacade::createTargetCondition('some condition');

php composer.phar