PHP code example of kedniko / firebase-cloud-messaging-http-v1-php

1. Go to this page and download the library: Download kedniko/firebase-cloud-messaging-http-v1-php 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/ */

    

kedniko / firebase-cloud-messaging-http-v1-php example snippets





$authKeyContent = json_decode(file_get_contents(__DIR__ . '/appname-30xfgre76.json'), true);
$projectID = 'my-project-1';
$body = [
    'message' => [
        'token' => '<token:string>',
        'notification' => [
            'title' => 'Breaking News',
            'body' => 'New news story available.',
        ],
        'data' => [
            'story_id' => 'story_12345',
        ],
    ],
];

$bearerToken = FCM::getBearerToken($authKeyContent);

FCM::send($bearerToken, $projectID, $body);





$authKeyContent = json_decode(file_get_contents(__DIR__ . '/appname-30xfgre76.json'), true);
$projectID = 'my-project-1';
$tokens = [
    '<token1:string>',
    '<token2:string>',
    '<token3:string>',
];

$bearerToken = FCM::getBearerToken($authKeyContent);

foreach ($tokens as $token) {
    $body = [
        'message' => [
            'token' => $token,
            'notification' => [
                'title' => 'Breaking News',
                'body' => 'New news story available.',
            ],
            'data' => [
                'story_id' => 'story_12345',
            ],
        ],
    ];

    FCM::send($bearerToken, $projectID, $body);
}





$authKeyContent = json_decode(file_get_contents(__DIR__ . '/appname-30xfgre76.json'), true);
$tokens = [
    '<token1:string>',
    '<token2:string>',
    '<token3:string>',
];

$bearerToken = FCM::getBearerToken($authKeyContent);
FCM::subscribeToTopic($bearerToken, 'my-topic-1', $tokens);





$authKeyContent = json_decode(file_get_contents(__DIR__ . '/appname-30xfgre76.json'), true);
$tokens = [
    '<token1:string>',
    '<token2:string>',
    '<token3:string>',
];

$bearerToken = FCM::getBearerToken($authKeyContent);
FCM::unsubscribeFromTopic($bearerToken, 'my-topic-1', $tokens);