PHP code example of baudev / fcm-xmpp

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

    

baudev / fcm-xmpp example snippets

  
class YOURCLASSNAME extends \FCMStream\Core {  
  
	public function onSend(string $from, string $messageId, Actions $actions) { 
		 // TODO: Implement onSend() method. 
	 }  
	 
	public function onReceipt(string $from, string $messageId, string $status, int $timestamp, Actions $actions)
         {
             // TODO: Implement onReceipt() method. 
         }
 
	public function onReceiveMessage($data, int $timeToLive, string $from, string $messageId, string $packageName, Actions $actions) { 
		// we answer to the message received 
		$message = new \FCMStream\Message();  
		$message->setTo($from);  
		$message->setMessageId("message_id_test");  
		$message->setPriority(\FCMStream\Message::PRIORITY_HIGH);  
		$message->setData(array("test" => "Hello World!")); 
		 
		$actions->sendMessage($message);
	}  
	
	/**
	 * The method is executed each X microseconds.
	 * To enable this method, you must execute enableOnLoopMethod()
	 * !! Warning !! Enabling this method can increase a lot the usage of your CPU!
	 * @param Actions $actions
	 */
	public function onLoop(Actions $actions)
	{
		// TODO: Implement onLoop() method. 
	}

	public function onFail(?string $error, ?string $errorDescription, ?string $from, ?string $messageId, Actions $actions) { 
		// TODO: Implement onFail() method. 
	}  
	
	public function onExpire(string $from, string $newFCMId, Actions $actions) { 
	    // TODO: Implement onExpire() method. 
	}
}  
  
$test = new YOURCLASSNAME('SENDER_ID', 'SERVER KEY', 'debugfile.txt', \FCMStream\helpers\Logs::DEBUG); 
// $test->enableOnLoopMethod(5 * 1000 * 1000); // enables the onLoop method. She will be called each 5 seconds. 
// Before uncommenting the previous line, see https://github.com/baudev/Firebase-Cloud-Messaging-FCM-XMPP/wiki/References#enableonloopmethodmicroseconds
$test->stream();  
  
$test = new FCMStream\Callbacks('SENDER_ID', 'SERVER KEY', 'debugfile.txt', \FCMStream\helpers\Logs::ANY);  
  
// onSend callback  
$test->setOnSend(function (string $from, string $messageId, Actions $actions){  
	// TODO: Implement onSend() method.
  });  
  
// onReceipt callback
$test->setOnReceipt(function (string $from, string $messageId, string $status, int $timestamp, Actions $actions) {
	// TODO: Implement onReceipt() method.
});
  
// onReceiveMessage callback  
$test->setOnReceiveMessage(function ($data, int $timeToLive, string $from, string $messageId, string $packageName, Actions $actions){ 
	// we answer to the message received 
	$message = new \FCMStream\Message();  
	$message->setTo($from);  
	$message->setMessageId("message_id_test");  
	$message->setPriority(\FCMStream\Message::PRIORITY_NORMAL);  
	$message->setData(array("test" => "Hello World!"));  
	
	$actions->sendMessage($message);  
});

// onLoop callback
// To enable this method, you must execute enableOnLoopMethod()
// !! Warning !! Enabling this method can increase a lot the usage of your CPU!
$test->setOnLoop(function (Actions $actions) {
	// TODO: Implement onLoop() method. 
});
  
// onFail callback  
$test->setOnFail(function (?string $error, ?string $errorDescription, ?string $from, ?string $messageId, Actions $actions) { 
	// TODO: Implement onFail() method. 
  });  
  
// onExpire callback  
$test->setOnExpire(function (string $from, string $newFCMId, Actions $actions){  
	// TODO: Implement onExpire() method. 
  });  
  
// $test->enableOnLoopMethod(5 * 1000 * 1000); // enables the onLoop method. She will be called each 5 seconds.
// Before uncommenting the previous line, see https://github.com/baudev/Firebase-Cloud-Messaging-FCM-XMPP/wiki/References#enableonloopmethodmicroseconds
$test->stream();