PHP code example of open-php / firebase-fcm-notification

1. Go to this page and download the library: Download open-php/firebase-fcm-notification 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/ */

    

open-php / firebase-fcm-notification example snippets




use OpenPhp\FirebaseFcm\FcmNotification;

try {
    // 1. Initialize with path to service account JSON
    $fcm = new FcmNotification('./service_account.json');

    // 2. Send a notification
    $title = "Hello World";
    $body = "This is a test notification";
    $token = "DEVICE_FCM_TOKEN"; 

    $result = $fcm->send($title, $body, $token);

    print_r($result);

} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

// Send to a topic
$isTopic = true;
$target = "news"; // Topic name
$imageUrl = "https://example.com/image.png";

$result = $fcm->send($title, $body, $target, $isTopic, $imageUrl);

firebase-fcm-notification/
├── src/
│   └── FcmNotification.php  <-- Main Library Class
├── composer.json            <-- Dependency Management
├── index.php                <-- Example Usage (HTTP)
└── service_account.json     <-- Your Firebase Credentials (NOT INCLUDED)