plokko/php-fcm-v1

此软件包最新版本(dev-master)没有可用的许可信息。

FCM v1 Api的PHP实现

dev-master 2018-03-08 15:03 UTC

This package is auto-updated.

Last update: 2024-09-18 02:11:34 UTC


README

FCM App服务器协议v1的PHP实现

注意:此项目现在是plokko/firebase-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';

以及系统特定的配置,如AndroidConfigWebpushConfigApnsConfig

消息需要指定Target参数,可以是单个设备(Token)、TopicCondition

$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);

如果该函数不会抛出FcmErrorBadRequest或通用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