PHP code example of maatify / google-fcm

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

    

maatify / google-fcm example snippets


use Maatify\FCM\FcmHandler;

_credentials_json_file_location__);

// Optional
$message->SetNotification('My Custom Title', 'My Custom Body', __image_url__ = '');

// Optional
$message->SetDate([
        'key1'=>'value1',
        'key2'=>'value2',
    ]);


try {
    // $message->sender cannot callable before setting at least one of optional setter
    $result = $message->sender->ToDeviceToken(__device_token__);
    
} catch (MessagingException|FirebaseException $e) {

    $result = (array) $e;
}

print_r($result);


// $message->sender cannot callable before setting at least one of optional setter
$result = $message->sender->ToMultipleDevicesToken([__device_token1__, __device_token2__]);

print_r($result);


try {
    // $message->sender cannot callable before setting at least one of optional setter
    $result = $message->sender->ToTopic(__topic__);
    
} catch (MessagingException|FirebaseException $e) {

    $result = (array) $e;
}

print_r($result);


try {

    $tokens = [__device_token__]; // to validate one token only
    $result = $message->TopicValidation()->ValidateRegistrationTokens($tokens);
    
} catch (MessagingException|FirebaseException $e) {

    $result = (array) $e;
}

print_r($result);


try {

    $tokens = [__device_token1__, __device_token2__]; // to validate many tokens
    $result = $message->TopicValidation()->ValidateRegistrationTokens($tokens);
    
} catch (MessagingException|FirebaseException $e) {

    $result = (array) $e;
}

print_r($result);

$topic_manager = $message->TopicManagement();

>   $registrationTokenOrTokens = __device_token__;
>   // or
>   $registrationTokenOrTokens = [__device_token1__, __device_token2__];
>   
 
>   $topic = 'topic-a';
>   
 
>   $topics = ['topic-a', 'topic-b'];
>   


$result = $topic_manager->SubscribeToTopic($topic, $registrationTokenOrTokens);

print_r($result);

$result = $topic_manager->SubscribeToTopics($topics, registrationTokenOrTokens);

print_r($result);


$result = $topic_manager->UnsubscribeFromTopic($topic, $registrationTokenOrTokens);

print_r($result);

$result = $topic_manager->SubscribeToTopics($topics, registrationTokenOrTokens);

print_r($result);

$result = $topic_manager->UnsubscribeFromAllTopics(registrationTokenOrTokens);

print_r($result);