plokko / php-fcm-v1
FCM v1 Api的PHP实现
Requires
- php: >=5.6
- google/auth: ^1.2.1
- guzzlehttp/guzzle: ^6.3
This package is auto-updated.
Last update: 2024-09-18 02:11:34 UTC
README
FCM App服务器协议v1的PHP实现
注意:此项目现在是
安装
使用composer通过以下命令安装
$composer require plokko\php-fcm-v1
使用方法
使用Message
类构建FCM消息
$message = new Message();
此类包含所有API V1对象,如Notification
//Set the message notification
$message->notification
->setTitle('My notification title')
->setBody('My notification body....');
数据负载Data
$message->data->fill([ 'a'=>1, 'b'=>'2', ]); $message->data->set('x','value'); $messsage->data->y='Same as above';
以及系统特定的配置,如AndroidConfig
、WebpushConfig
和ApnsConfig
。
消息需要指定Target
参数,可以是单个设备(Token
)、Topic
或Condition
$message->setTarget(new Token('your-fcm-device-token')); //Or $message->setTarget(new Topic('your-fcm-topic')); //Or $message->setTarget(new Condition('your-fcm-conditions'));
如果此值未设置,则在提交之前消息将抛出错误。
在提交之前,我们需要下载包含您的服务账户凭据的JSON文件(请参阅https://firebase.google.com/docs/admin/setup#initialize_the_sdk)。
此文件用于初始化用于生成FCM请求OAuth2令牌的ServiceAccount
类。
// Prepare service account $sa = new ServiceAccount('path/to/your/firebase-credentials.json');
ServiceAccount
不会直接用于提交消息,而是用于构建Request
$request = new Request($sa);
出于测试目的,您还可以将validate_only
参数设置为在提交到设备之前在Firebase中测试消息;也可以用自定义GuzzleHttp客户端覆盖请求的HTTP客户端。
//Custom http client $myClient = new Client(['debug'=>true]); $request->setHttpClient($myClient); $request->validate_only = true; //or $request->validateOnly(true);
使用send
方法发送Message
$message->send($request);
如果该函数不会抛出FcmError
、BadRequest
或通用GuzzleException
,则表示消息已成功发送,并且如果它不是一个validate_only请求,则消息名称将被填充。
//after submitting: echo 'my message name:'.$message->name; `` If you want to use the validate_only without modifying the request the `validate` method will force the validate_only flag on the request. ```php $request->validateOnly(false); $message->validate($request);//will be a validate_only request anyway
完整示例
<?php require dirname(__DIR__) . '/vendor/autoload.php'; use GuzzleHttp\Client; use Plokko\PhpFcmV1\Message; use Plokko\PhpFcmV1\Request; use Plokko\PhpFcmV1\ServiceAccount; use Plokko\PhpFcmV1\Targets\Token; // Your Firebase credential file path $firebase_secrets_file = dirname(__DIR__).'/.firebase-credentials.json'; // Prepare service account $sa = new ServiceAccount($firebase_secrets_file); $message = new Message(); // Add a data payload,NB: everything will be converted as a string:string $message->data->fill([ 'a'=>1,//Will be converted to '1' 'b'=>'2', 'x'=>'xxxxxxxxxxxxxxx', 'n'=>null,//Will be converted to '' ]); //Add a notification $message->notification ->setTitle('My notification title') ->setBody('My notification body....'); //Set the message destination $message->setTarget(new Token('your-device-token')); //Custom http client (optional) $myClient = new Client(['debug'=>true]); //Prepare the request $request = new Request($sa,true,$myClient); $message->send($request);
故障排除
如果您收到403错误“在项目 <项目名称> 中之前尚未使用Firebase Cloud Messaging API...”,那是因为新版本v1 API没有自动启用(即使您已从Firebase控制台生成凭据,且旧版Api仍可工作)。
您需要从以下页面启用它:https://console.developers.google.com/apis/api/fcm.googleapis.com/overview