ker0x/fcm

一个用于通过 Firebase Cloud Messaging 发送推送通知的 PHP 库

3.2.0 2024-02-15 09:41 UTC

This package is auto-updated.

Last update: 2024-09-07 10:24:33 UTC


README

Tests Coverage PHP Version Download Packagist License

Fcm

一个用于通过 Firebase Cloud Messaging 发送推送通知的 PHP 库

注意

此库的 3.x 版本完全重写,使用 PSR-18 HTTP 客户端 接口,这意味着库内不提供任何 HTTP 客户端,如 Guzzlehttplug。如果您的项目中已经有了一个,该包将 自动发现 并使用它。否则,您需要单独要求一个。

警告

3.2 版本引入了 BC 不兼容性。Kerox\Fcm\Model\Message 类的 __construct() 方法的签名已更改,其中 $notification 参数成为第三个参数,且为可选。

安装

您可以使用 Composer 安装 Fcm

composer require kerox/fcm

然后您需要

  • 运行 composer install 以将这些依赖项添加到您的 vendor 目录
  • 使用以下行将自动加载器添加到您的应用程序中: require('vendor/autoload.php');

基本用法

use Kerox\Fcm\Fcm;
use Kerox\Fcm\Model\Message;
use Kerox\Fcm\Model\Notification\Notification;
use Kerox\Fcm\Model\Target;

$fcm = new Fcm('<oauth_token>', '<project_id>');

// Create the message
$message = new Message(
    target: new Token('TopicA'),
    data: [
        'story_id' => 'story_12345',
    ],
    notification: new Notification(
        title: 'Hello World',
        body: 'My awesome Hello World!'
    ),
)

// Send the message and get the response
$response = $fcm->send()->message($message);

文档

文档在此处可用 https://github.com/ker0x/fcm/wiki

测试

要现场测试此包,您必须首先生成一个 OAuth 令牌。访问 https://developers.google.com/oauthplayground/ 并从 API 列表中选择 Firebase Cloud Messaging API v1。然后选择 https://www.googleapis.com/auth/firebase.messaging 并生成 OAuth 令牌。最后,定义一个名为 FCM_OAUTH_TOKEN 的环境变量,并将其值设置为访问令牌。