baudev/fcm-xmpp

Allows XMPP connection with Firebase Cloud Messaging. It can listen upstream messages and send downstream ones.

1.1.0 2019-08-01 13:31 UTC

This package is auto-updated.

Last update: 2024-05-29 03:57:26 UTC


README

Service Master Develop
CI Status Build Status Build Status
Version Master version Develop version
PHP version PHP MINIMUM VERSION PHP MINIMUM VERSION

This PHP program, based on the unmaintained sourc7/FCMStream repository, allows receiving and sending messages with the XMPP Protocol using Firebase Cloud Messaging.

INSTALLATION

composer require baudev/fcm-xmpp  

EXAMPLES

  • Create an index.php file and write into it one of the two following script (method1 or method2). Don't forget replacing : SENDER_ID, SERVER KEY.
  • Run the script: php index.php

Note: examples are provided in the directory examples. More explanations can be found into the wiki page.

1. Using a class (best solution):

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();  

2. Using function callback parameters:

$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();  

USAGE

  1. Downstream Messages: server-to-device through FCM

  1. Upstream Messages: device-to-server through FCM

DOCUMENTATION

See the wiki page to discover every possibilities provided by this framework.

TODO

  • Add more comments
  • Add methods for responding easily, to set message priority and so on. Notification property is not handled yet
  • Improve README
  • Add more tests

CREDITS

LICENSE

MIT License  
  
Copyright (c) 2016 Ante Radman (Radoid), Irvan Kurniawan (TechRapid), Baudev.  
  
Permission is hereby granted, free of charge, to any person obtaining a copy  
of this software and associated documentation files (the "Software"), to deal  
in the Software without restriction, including without limitation the rights  
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
copies of the Software, and to permit persons to whom the Software is  
furnished to do so, subject to the following conditions:  
  
The above copyright notice and this permission notice shall be included in all  
copies or substantial portions of the Software.  
  
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  
SOFTWARE.