PHP code example of strivekp / phpfcmsb

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

    

strivekp / phpfcmsb example snippets


  
  pFCMSBv1\Client;
  use phpFCMSBv1\Notification;
  use phpFCMSBv1\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;
  

  
  pFCMSBv1\Client;
  use phpFCMSBv1\Notification;
  use phpFCMSBv1\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();
  

  
  pFCMSBv1\Client;
  use phpFCMSBv1\Config;
  use phpFCMSBv1\Notification;
  use phpFCMSBv1\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();
  

  // Option Instance for Android
  // Use phpFCMSBv1\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 phpFCMSBv1\APNsCOnfig Class
  $apnsConfig = new APNsConfig();
  $apnsConfig -> setPriority(APNsConfig::PRIORITY_HIGH);
  $client -> build($recipient, $notification, null, $apnsConfig);