strivekp / phpfcmsb
PHP 实现的 FCM HTTP v1 API,更新为使用 guzzle 7
v1.0.2
2019-07-17 07:39 UTC
Requires
- php: >=5.6
- firebase/php-jwt: ^5.0
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-19 13:56:28 UTC
README
phpfcmsb 是 FCM HTTP v1 API 的 PHP 实现,对应 FCM
与其他 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 strivekp/phpfcmsb
-
使用 autoload.php 导入库
<?php require_once __DIR__ . '/vendor/autoload.php'; use phpFCMSBv1\Client; use phpFCMSBv1\Notification; use phpFCMSBv1\Recipient;
-
创建必要的类实例:Client、Recipient、Notification/Data
// 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 phpFCMSBv1\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();
-
使用 PRIOIRTY 选项(适用于 Android 和 iOS)
<?php require_once __DIR__ . '/vendor/autoload.php'; use phpFCMSBv1\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();
-
对于独立平台(Android 或 iOS)
// 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);
未来工作
- 实现同时发送(目前支持一次处理单个接收者或主题)
- 设置 Read the Docs
- 添加 CI 测试
- 添加 CodeCov 徽章