ermakove / php-firebase-cloud-messaging
来自Google的Firebase云消息传递的PHP API
dev-master
2019-06-13 10:32 UTC
Requires
- php: >=5.5
- guzzlehttp/guzzle: *
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-13 21:56:13 UTC
README
来自Google的Firebase云消息传递的PHP API。
目前此应用程序服务器库仅支持通过HTTP发送消息/通知。
查看原始Firebase文档:https://firebase.google.com/docs/
#设置 安装通过Composer
composer require sngrl/php-firebase-cloud-messaging
或者将其添加到你的composer.json中并运行"composer update"
"require": {
"sngrl/php-firebase-cloud-messaging": "dev-master"
}
#向设备发送消息
use ermakove\PhpFirebaseCloudMessaging\Client;
use ermakove\PhpFirebaseCloudMessaging\Message;
use ermakove\PhpFirebaseCloudMessaging\Recipient\Device;
use ermakove\PhpFirebaseCloudMessaging\Notification;
$server_key = '_YOUR_SERVER_KEY_';
$client = new Client();
$client->setApiKey($server_key);
$client->injectGuzzleHttpClient(new \GuzzleHttp\Client());
$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());
#向多个设备发送消息
...
$message = new Message();
$message->setPriority('high');
$message->addRecipient(new Device('_YOUR_DEVICE_TOKEN_'));
$message->addRecipient(new Device('_YOUR_DEVICE_TOKEN_2_'));
$message->addRecipient(new Device('_YOUR_DEVICE_TOKEN_3_'));
$message
->setNotification(new Notification('some title', 'some body'))
->setData(['key' => 'value'])
;
...
#向主题发送消息
use ermakove\PhpFirebaseCloudMessaging\Client;
use ermakove\PhpFirebaseCloudMessaging\Message;
use ermakove\PhpFirebaseCloudMessaging\Recipient\Topic;
use ermakove\PhpFirebaseCloudMessaging\Notification;
$server_key = '_YOUR_SERVER_KEY_';
$client = new Client();
$client->setApiKey($server_key);
$client->injectGuzzleHttpClient(new \GuzzleHttp\Client());
$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());
#向多个主题发送消息
有关向多个主题组合发送消息的Firebase文档:https://firebase.google.com/docs/cloud-messaging/topic-messaging#sending_topic_messages_from_the_server
...
$message = new Message();
$message->setPriority('high');
$message->addRecipient(new Topic('_YOUR_TOPIC_'));
$message->addRecipient(new Topic('_YOUR_TOPIC_2_'));
$message->addRecipient(new Topic('_YOUR_TOPIC_3_'));
$message
->setNotification(new Notification('some title', 'some body'))
->setData(['key' => 'value'])
// Will send to devices subscribed to topic 1 AND topic 2 or 3
->setCondition('%s && (%s || %s)')
;
...
#订阅用户到主题
use ermakove\PhpFirebaseCloudMessaging\Client;
$server_key = '_YOUR_SERVER_KEY_';
$client = new Client();
$client->setApiKey($server_key);
$client->injectGuzzleHttpClient(new \GuzzleHttp\Client());
$response = $client->addTopicSubscription('_SOME_TOPIC_ID_', ['_FIRST_TOKEN_', '_SECOND_TOKEN_']);
var_dump($response->getStatusCode());
var_dump($response->getBody()->getContents());
#取消用户对主题的订阅
use ermakove\PhpFirebaseCloudMessaging\Client;
$server_key = '_YOUR_SERVER_KEY_';
$client = new Client();
$client->setApiKey($server_key);
$client->injectGuzzleHttpClient(new \GuzzleHttp\Client());
$response = $client->removeTopicSubscription('_SOME_TOPIC_ID_', ['_FIRST_TOKEN_', '_SECOND_TOKEN_']);
var_dump($response->getStatusCode());
var_dump($response->getBody()->getContents());