PHP code example of dimajolkin / symfony-firebase-notifier

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

    

dimajolkin / symfony-firebase-notifier example snippets




use Dimajolkin\SymfonyFirebaseNotifier\Credential;
use Dimajolkin\SymfonyFirebaseNotifier\FirebaseTransport;
use Dimajolkin\SymfonyFirebaseNotifier\MessageType\TargetMessageOptions;
use Dimajolkin\SymfonyFirebaseNotifier\Notification\AndroidNotification;
use Dimajolkin\SymfonyFirebaseNotifier\Notification\CommonNotification;
use Symfony\Component\Notifier\Chatter;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;

$file = file_get_contents('......json');

$token = Credential::fromServiceAccountContent($file)->getToken();

$chatter = new Chatter(new FirebaseTransport($token));

$chatMessage = new ChatMessage('super text');

$options = new TargetMessageOptions(
    token: '....',
    common: new CommonNotification(
      title: 'incident title',
      body: 'test message',
    ),
    android: new AndroidNotification(
        clickAction: 'open_incident_view',
    ),
    data: [
        'id' => '54841',
        'type' => 'incident',
        'title' => 'incident title',
        'message' => 'incident message',
    ],
);

// Add the custom options to the chat message and send the message
$chatMessage->options($options);
try {
    $chatter->send($chatMessage);
} catch (TransportException $exception) {
    echo $exception->getMessage();
}