baudev/fcm-xmpp

允许使用Firebase Cloud Messaging进行XMPP连接。它可以监听上游消息并发送下游消息。

1.1.0 2019-08-01 13:31 UTC

This package is auto-updated.

Last update: 2024-09-29 04:49:44 UTC


README

这个PHP程序基于不再维护的sourc7/FCMStream存储库,允许使用XMPP协议通过Firebase Cloud Messaging接收和发送消息。

安装

composer require baudev/fcm-xmpp  

示例

  • 创建一个index.php文件,并在其中写入以下两个脚本之一(方法1方法2)。别忘了替换:SENDER_IDSERVER KEY
  • 运行脚本:php index.php

注意:示例在examples目录中提供。更多信息可以在wiki页面中找到。

1. 使用类(最佳方案

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. 使用函数回调参数

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

使用方法

  1. 下游消息:通过FCM从服务器到设备

  1. 上游消息:通过FCM从设备到服务器

文档

查看wiki页面以了解该框架提供的所有功能。

待办事项

  • 添加更多注释
  • 添加易于响应的方法,例如设置消息优先级等。尚未处理通知属性
  • 改进README
  • 添加更多测试

致谢

  • 使用部分中的图片来自XAMARIN文档
  • 代码的大部分来自sourc7/FCMStream存储库。由于它不再维护,我允许自己分叉并改进它。

许可证

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.