cpt-sugiura / php-fcm-v1
PHP实现的FCM HTTP v1 API
dev-master
2024-09-18 04:02 UTC
Requires
- php: >=5.6
- ext-json: *
- firebase/php-jwt: ^5.0
- guzzlehttp/guzzle: ^6.0|^7.0
Requires (Dev)
- phpunit/php-code-coverage: ^10
- phpunit/phpunit: ^10
This package is auto-updated.
Last update: 2024-09-18 04:03:15 UTC
README
php-fcm-v1是FCM HTTP v1 API的PHP实现
与其他FCM库相比有何不同?
大多数其他库是FCM的旧版HTTP服务器协议的实现。它需要从Firebase控制台获取服务器密钥(这意味着您需要在代码中复制和粘贴)(文档)
相比之下,HTTP v1 API利用OAuth2安全模型。您需要获取一个访问令牌(有效期为约一小时)才能使用服务账户的私钥文件请求发送通知。(请参阅有关HTTP v1 API的博客文章)
参考资料
- google/node-gtoken
- google/google-auth-library-nodejs:上述两个库让我了解了FCM中HTTP v1 API的工作方式
- guzzlehttp/guzzle:GuzzleHttp使得这个库与PSR7兼容
- Paragraph1/php-fcm:启发我了解如何在旧版HTTP协议中使用FCM库
如何使用
-
使用composer安装库
composer require lkaybob/php-fcm-v1
-
使用autoload.php导入库
<?php require_once __DIR__ . '/vendor/autoload.php'; use phpFCMv1\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);
-
在FCM服务器上执行!
$result = $client -> fire(); // You can check the result // If successful, true will be returned // If not, error message will be returned echo $result;
进一步示例
-
完整简单示例
<?php require_once __DIR__ . '/vendor/autoload.php'; use phpFCMv1\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();
-
使用PRIOIRTY选项(适用于Android和iOS)
<?php require_once __DIR__ . '/vendor/autoload.php'; use phpFCMv1\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();
-
对于独立平台(无论是Android还是iOS)
// 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);
未来工作
- 实现同时发送(目前支持一次发送单个接收者或主题)
- 设置Read the Docs
- 添加CI测试
- 添加CodeCov徽章