redjanym/php-firebase-cloud-messaging

来自Google的Firebase云消息的PHP API

v1.1.7 2022-07-14 18:56 UTC

This package is auto-updated.

Last update: 2024-09-14 23:47:21 UTC


README

SensioLabsInsight Buy Me a Coffee at ko-fi.com

来自Google的Firebase云消息的PHP API。

目前此应用程序服务器库仅支持通过HTTP发送消息/通知。

查看原始Firebase文档:https://firebase.google.com/docs/

#安装

composer require redjanym/php-firebase-cloud-messaging

或者将其添加到您的composer.json中并运行"composer update"

"require": {
    "redjanym/php-firebase-cloud-messaging": "1.*"
}

向一个或多个设备发送消息

use sngrl\PhpFirebaseCloudMessaging\Client;
use sngrl\PhpFirebaseCloudMessaging\Message;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Device;
use sngrl\PhpFirebaseCloudMessaging\Notification;

$server_key = '_YOUR_SERVER_KEY_';
$client = new Client();
$client->setApiKey($server_key);

$message = new Message();
$message->setPriority('high');
$message->addRecipient(new Device('_YOUR_DEVICE_TOKEN_'));
$message
    ->setNotification(new Notification('some title', 'some body'))
    ->setData(['key' => 'value'])
;

$response = $client->send($message);
var_dump($response->getStatusCode());
var_dump($response->getBody()->getContents());

向主题发送消息

目前向主题发送消息仅支持单个主题作为接收者。根据谷歌文档中的描述,多个主题似乎还不能使用。

use sngrl\PhpFirebaseCloudMessaging\Client;
use sngrl\PhpFirebaseCloudMessaging\Message;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Topic;
use sngrl\PhpFirebaseCloudMessaging\Notification;

$server_key = '_YOUR_SERVER_KEY_';
$client = new Client();
$client->setApiKey($server_key);

$message = new Message();
$message->setPriority('high');
$message->addRecipient(new Topic('_YOUR_TOPIC_'));
$message
    ->setNotification(new Notification('some title', 'some body'))
    ->setData(['key' => 'value'])
;

$response = $client->send($message);
var_dump($response->getStatusCode());
var_dump($response->getBody()->getContents());

将用户订阅到主题

use sngrl\PhpFirebaseCloudMessaging\Client;

$server_key = '_YOUR_SERVER_KEY_';
$client = new Client();
$client->setApiKey($server_key);

$response = $client->addTopicSubscription('_SOME_TOPIC_ID_', ['_FIRST_TOKEN_', '_SECOND_TOKEN_']);
var_dump($response->getStatusCode());
var_dump($response->getBody()->getContents());

取消用户对主题的订阅

use sngrl\PhpFirebaseCloudMessaging\Client;

$server_key = '_YOUR_SERVER_KEY_';
$client = new Client();
$client->setApiKey($server_key);

$response = $client->removeTopicSubscription('_SOME_TOPIC_ID_', ['_FIRST_TOKEN_', '_SECOND_TOKEN_']);
var_dump($response->getStatusCode());
var_dump($response->getBody()->getContents());

解释响应

HTTP请求的响应是根据FCM文档规范的标准。您可以在以下链接中找到详细说明