trantuanson1991 / php-fcm-v1
PHP 实现的 FCM HTTP v1 API。基于 lkaybob/php-fcm-v1 改进,并更换 Firebase JWT 和 Guzzle 版本
v2.0.1
2024-08-20 03:20 UTC
Requires
- php: >=7.1
- firebase/php-jwt: ^6.2
- guzzlehttp/guzzle: ^7.5
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-20 03:43:33 UTC
README
php-fcm-v1 是 FCM HTTP v1 API 的 PHP 实现,基于 FCM
与其它 FCM 库相比有哪些不同?
大多数其他库都是 FCM 的 旧版 HTTP 服务器协议 实现。它需要从 Firebase 控制台中获取服务器密钥(这意味着您需要在代码中复制和粘贴)(文档)
与旧版 HTTP 服务器协议相比,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 trantuanson1991/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 徽章