PHP code example of trantuanson1991 / php-fcm-v1

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

    

trantuanson1991 / php-fcm-v1 example snippets


  
  pFCMv1\Client;
  use phpFCMv1\Notification;
  use phpFCMv1\Recipient;
  

  // Client instance should be created with path to service account key file
  $client = new Client('service_account.json');
  $recipient = new Recipient();
  // Either Notification or Data (or both) instance should be created
  $notification = new Notification();
  

  // Recipient could accept individual device token,
  // the name of topic, and conditional statement
  $recipient -> setSingleREcipient('DEVICE_TOKEN');
  // Setup Notificaition title and body
  $notification -> setNotification('NOTIFICATION_TITLE', 'NOTIFICATION_BODY');
  // Build FCM request payload
  $client -> build($recipient, $notification);
  

  $result = $client -> fire();
  // You can check the result
  // If successful, true will be returned
  // If not, error message will be returned
  echo $result;
  

  
  pFCMv1\Client;
  use phpFCMv1\Notification;
  use phpFCMv1\Recipient;

  $client = new Client('service_account.json');
  $recipient = new Recipient();
  $notification = new Notification();

  $recipient -> setSingleRecipient('DEVICE_TOKEN');
  $notification -> setNotification('NOTIFICATION_TITILE', 'NOTIFICATION_BODY');
  $client -> build($recipient, $notification);
  $client -> fire();
  

  
  pFCMv1\Client;
  use phpFCMv1\Config;
  use phpFCMv1\Notification;
  use phpFCMv1\Recipient;

  $client = new Client('service_account.json');
  $recipient = new Recipient();
  $notification = new Notification();
  $config = new Config();

  $recipient -> setSingleRecipient('DEVICE_TOKEN');
  $notification -> setNotification('NOTIFICATION_TITLE', 'NOTIFICATION_BODY');
  $config -> setPriority(Config::PRIORITY_HIGH);
  $client -> build($recipient, $notification, null, $config);
  $result = $client -> fire();
  

  composer 

  // Option Instance for Android
  // Use phpFCMv1\AndroidConfig Class
  $androidConfig = new Config\AndroidConfig();
  $androidConfig -> setPriority(Config\AndroidConfig::PRIORITY_HIGH);
  $client -> build($recipient, $notification, null, $androidConfig);
  
  // Option Instance for iOS (which is APNs header)
  // Use phpFCMv1\APNsCOnfig Class
  $apnsConfig = new APNsConfig();
  $apnsConfig -> setPriority(APNsConfig::PRIORITY_HIGH);
  $client -> build($recipient, $notification, null, $apnsConfig);