PHP code example of mve / fcm-php

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

    

mve / fcm-php example snippets


$messaging = new Messaging(new MyCache(), '/path/to/file.json', Log::getLogger());

// Create messages. The first argument is the ID - this can be used to map the result to the specific message after sending.
$messages = [
    new TokenMessage(1, 'token1', "Content of notification", "Title of notification"),
    new TokenMessage(2, 'token1', "Content of notification", "Title of notification")
];
// Send the messages
try {
    $sendResult = $messaging->sendAll($messages);
    foreach ($sendResult->getSent() as $messageId => $firebaseId) {
        // Successfully sent messages
    }
    foreach ($sendResult->getUnregistered() as $messageId => $fcmError) {
        // The tokens for these messages can be deleted safely.
    }
    foreach ($sendResult->getErrors() as $messageId => $fcmError) {
        // See the $fcmError for specifics about this error.
    }
} catch (\Mve\FcmPhp\Models\FcmException $ex) {
    // This is bad, but it's at least an error we got from the Google API
    die('Exception from the Google API: ' . $ex->fcmError->content);
} catch (\Exception $ex) {
    // This is also bad, and it's a non Google API exception, maybe network error?
    die('Some other exception: ' . $ex->getMessage());
}