PHP code example of blutrixx / nativephp-fcm

1. Go to this page and download the library: Download blutrixx/nativephp-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/ */

    

blutrixx / nativephp-fcm example snippets


// Server side — a real send, once you have a token in your own device-tokens table.
// See SendNotificationJob::sendFcmPush() in this test app's BACKEND for the full version
// (handles token deactivation on failed sends, batching, etc.) — the essential call:
use Google\Auth\Credentials\ServiceAccountCredentials;
use Illuminate\Support\Facades\Http;

$credentials = new ServiceAccountCredentials(
    'https://www.googleapis.com/auth/firebase.messaging',
    json_decode(file_get_contents(config('firebase.credentials_file')), true)
);
$accessToken = $credentials->fetchAuthToken()['access_token'];

Http::withToken($accessToken)->post(
    'https://fcm.googleapis.com/v1/projects/' . config('firebase.project_id') . '/messages:send',
    ['message' => ['token' => $deviceToken, 'data' => ['title' => 'Hello', 'body' => 'It works']]]
);