PHP code example of symfony / firebase-notifier

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

    

symfony / firebase-notifier example snippets




if (!isset($argv[1])) {
    echo 'Usage: php ' . $argv[0] . ' path_to_your_key.json'.PHP_EOL;
    exit(1);
}

$file = file_get_contents($argv[1]);

if (false === $file) {
    echo 'Could not open file at path: '.$argv[1].PHP_EOL.PHP_EOL;
    exit(1);
}

$key = json_decode($file, true);

if (false === $key) {
    echo 'Unable to load JSON content of the file at path: '.$argv[1].PHP_EOL;
    exit(1);
}

foreach (['client_email', 'project_id', 'private_key_id', 'private_key'] as $param) {
    if (!isset($key[$param])) {
        echo 'Missing param '.$param.' inside the JSON key.'.PHP_EOL;
        exit(1);
    }
}

echo sprintf(
    'FIREBASE_DSN=firebase://%s@default?project_id=%s&private_key_id=%s&private_key=%s',
    $key['client_email'],
    $key['project_id'],
    $key['private_key_id'],
    urlencode($key['private_key']),
).PHP_EOL;

use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseOptions;

$chatMessage = new ChatMessage('Hello, you should contribute to Symfony.');

// Specify options for Firebase
$firebaseOptions = (new FirebaseOptions('/topics/news'))
    ->title('New message!')
    ->body('This will overwrite the subject from ChatMessage object.')
    ->image('https://path-to-your-image.png')
    ->data([
        'key1' => 'value1',
        'key2' => 'value2',
    ])
    // ...
    ;

// Add the custom options to the chat message and send the message
$chatMessage->options($androidOptions);

$chatter->send($chatMessage);

use Symfony\Component\Notifier\Bridge\Firebase\FirebaseOptions;
use Symfony\Component\Notifier\Bridge\Firebase\TargetType;

// Use a topic as a target
$topicOptions = new FirebaseOptions('/topics/news', targetType: TargetType::Topic);

// Use a token as a target
$tokenOptions = new FirebaseOptions('dU5e3nFJf9bE:APA91bH3Kd/exampleToken', targetType: TargetType::Token);

// Use a condition as a target
$conditionOptions = new FirebaseOptions('\'news\' in topics', targetType: TargetType::Condition);

use Symfony\Component\Notifier\Bridge\Firebase\FirebaseOptions;
use Symfony\Component\Notifier\Bridge\Firebase\TargetType;

$detailedOptions = (new FirebaseOptions('dU5e3nFJf9bE:APA91bH3Kd/exampleToken', targetType: TargetType::Token))
    // Basic options
    ->title('New message!')
    ->data([
        'key1' => 'value1',
        'key2' => 'value2',
    ])
    // Android specific options
    ->android([
        'notification' => [
            'color' => '#4538D5',
        ],
    ])
    // WebPush specific options
    ->webpush([
        'notification' => [
            'icon' => 'https://path-to-an-icon.png',
        ],
    ])
    // APNS specific options
    ->apns([
        'payload' => [
            'aps' => [
                'sound' => 'default',
            ],
        ],
    ])
    // ...
    ;
bash
php script_name.php path/to/your/firebase/key.json