granam / php-firebase-cloud-messaging
来自谷歌的Firebase Cloud Messaging的PHP API
2.2.0
2018-04-19 12:16 UTC
Requires
- php: >=7.1
- granam/strict-object: ^3.0
- guzzlehttp/guzzle: ^6.3
Requires (Dev)
- granam/exceptions-hierarchy: ^4.0
- granam/string: ^2.2
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.0
- roave/security-advisories: dev-master
README
PHP API for Firebase Cloud Messaging from Google.
此库仅支持通过HTTP发送消息/通知。
需要 PHP 7.1,如果您必须依赖较低版本,请使用 原始PHP FCM库 或 它的某个克隆版本。
向设备或主题发送消息
<?php use granam\FirebaseCloudMessaging\FcmClient; use granam\FirebaseCloudMessaging\FcmMessage; use granam\FirebaseCloudMessaging\Target\FcmTopicTarget; use granam\FirebaseCloudMessaging\Target\FcmDeviceTarget; use granam\FirebaseCloudMessaging\AndroidFcmNotification; $client = new FcmClient(new \GuzzleHttp\Client(), '_YOUR_SERVER_KEY_'); $message = new FcmMessage(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_')); $message->setPriority('high') ->setNotification(new AndroidFcmNotification('A message from Foo!', 'Hi Bar, how are you?')) ->setData(['photo' => 'https://example.com/photos/Foo.png']); // you can NOT combine multiple recipient types as topic and device in a single message $messageForTopic = new FcmMessage(new FcmTopicTarget('_YOUR_TOPIC_')); $messageForTopic ->setNotification(new AndroidFcmNotification('Foo is looking for you!', 'Here you are!')) ->setData(['map' => 'https://example.com/maps/foo']); $response = $client->send($message); var_dump($response); $responseFromTopic = $client->send($messageForTopic); var_dump($responseFromTopic);
向设备发送静默推送通知
应用将需要自行处理通知,因为系统不会显示它。
<?php use granam\FirebaseCloudMessaging\FcmClient; use granam\FirebaseCloudMessaging\FcmMessage; use granam\FirebaseCloudMessaging\Target\FcmDeviceTarget; $client = new FcmClient(new \GuzzleHttp\Client(), '_YOUR_SERVER_KEY_'); $message = new FcmMessage(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_')); $message->setData([ 'title' => 'A message from Foo!', 'message' => 'Hi Bar, how are you?', 'photo' => 'https://example.com/photos/Foo.png' ]); $response = $client->send($message); var_dump($response);
向多个设备发送消息
<?php use granam\FirebaseCloudMessaging\FcmMessage; use granam\FirebaseCloudMessaging\Target\FcmDeviceTarget; use granam\FirebaseCloudMessaging\IosFcmNotification; $message = new FcmMessage(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_')); $message->addTarget(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_2_')) ->addTarget(new FcmDeviceTarget('_YOUR_DEVICE_TOKEN_3_')) ->setPriority('high') ->setNotification(new IosFcmNotification('You are wanted', 'We got some issue here, where are you? We need you.')) ->setData(['attachment' => 'data:image/gif;base64,FooBar==']); // ...
向多个主题发送消息
有关向多个主题组合发送消息的说明,请参阅Firebase文档 combinations of multiple topics。
<?php use granam\FirebaseCloudMessaging\FcmMessage; use granam\FirebaseCloudMessaging\JsFcmNotification; use granam\FirebaseCloudMessaging\Target\FcmTopicTarget; $message = new FcmMessage(new FcmTopicTarget('_YOUR_TOPIC_')); $message->addTarget(new FcmTopicTarget('_YOUR_TOPIC_2_')) ->addTarget(new FcmTopicTarget('_YOUR_TOPIC_3_')) ->setPriority('high') ->setNotification(new JsFcmNotification('some title', 'some body')) ->setData(['key' => 'value']) // will send to devices subscribed to topic 1 AND topic 2 or 3 ->setCondition('%s && (%s || %s)');
将用户订阅到主题
<?php use granam\FirebaseCloudMessaging\FcmClient; $client = new FcmClient(new \GuzzleHttp\Client(), '_YOUR_SERVER_KEY_'); $response = $client->subscribeToTopic('_SOME_TOPIC_', ['_FIRST_DEVICE_TOKEN_', '_SECOND_DEVICE_TOKEN_']); var_dump($response->getStatusCode()); var_dump($response->getBody()->getContents());
从主题中取消用户订阅
<?php use granam\FirebaseCloudMessaging\FcmClient; $client = new FcmClient(new \GuzzleHttp\Client(), '_YOUR_SERVER_KEY_'); $response = $client->unsubscribeFromTopic('_SOME_TOPIC_', ['_FIRST_DEVICE_TOKEN_', '_SECOND_DEVICE_TOKEN_']); var_dump($response->getStatusCode()); var_dump($response->getBody()->getContents());
安装
composer require granam/php-firebase-cloud-messaging