lkaybob/php-fcm-v1

PHP实现的FCM HTTP v1 API

v1.0.2 2019-07-17 07:39 UTC

This package is auto-updated.

Last update: 2024-08-30 01:26:52 UTC


README

Build Status codecov Latest Stable Version Total Downloads License

php-fcm-v1是FCM HTTP v1 API的PHP实现

与其他FCM库相比有何不同?

大多数其他库都是FCM的旧版HTTP服务器协议的实现。它需要从Firebase控制台获取服务器密钥(这意味着您需要在代码中复制和粘贴)(文档

相比之下,HTTP v1 API利用OAuth2安全模型。您需要获取一个访问令牌(有效期为约一小时)才能使用服务账户的私钥文件请求发送通知。尽管(参见有关HTTP v1 API的博客帖子

参考

如何使用

  • 使用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、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 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徽章