From f04fdc826456150a2a11303c94b7163b47c7b070 Mon Sep 17 00:00:00 2001 From: Abhishek Sachan Date: Sun, 27 Sep 2020 19:03:27 +0330 Subject: [PATCH] Add Topics Creation And Subscription Co-authored-by: Steve Moretz Co-authored-by: William Desportes --- README.md | 33 +++++++++++++ config/fcm.php | 1 + src/FCMServiceProvider.php | 21 +++++++- src/Facades/FCMTopic.php | 17 +++++++ src/Facades/FCMValidator.php | 15 ++++++ src/Request/TopicRequest.php | 67 +++++++++++++++++++++++++ src/Request/ValidateRequest.php | 15 ++++++ src/Sender/FCMTopic.php | 86 +++++++++++++++++++++++++++++++++ src/Validator/FCMValidator.php | 32 ++++++++++++ 9 files changed, 286 insertions(+), 1 deletion(-) create mode 100644 src/Facades/FCMTopic.php create mode 100644 src/Facades/FCMValidator.php create mode 100644 src/Request/TopicRequest.php create mode 100644 src/Request/ValidateRequest.php create mode 100644 src/Sender/FCMTopic.php create mode 100644 src/Validator/FCMValidator.php diff --git a/README.md b/README.md index 19288f193..8428e9f25 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,32 @@ $topicResponse->error()); ``` +#### Creating a Topic + +```php +$token = 'device_id'; +$topic_id = 'unique_topic_id'; //unique topic id. +// Save notification key in your database you must use it to send messages or for managing this group +$notification_key = FCMTopic::createTopic($topic_id, $token); +``` + +#### Subscribe to a Topic + +```php +$recipients_tokens = ['device_id', '...']; +$topic_id = 'unique_topic_id'; +$key = FCMTopic::subscribeTopic($topic_id, $recipients_tokens); +``` + +#### UnSubscribe to a Topic + +```php +$recipients_tokens = ['device_id', '...']; +$topic_id = 'unique_topic_id'; +$key = FCMTopic::unsubscribeTopic($topic_id, $recipients_tokens); +``` + + ### Group Messages #### Sending a Notification to a Group @@ -408,6 +434,13 @@ $topics->topic('TopicA') }); ``` +## Validating + +### Validate FCM Token + +```php +$isValid = FCMValidator::validateToken($token); +``` ## Testing diff --git a/config/fcm.php b/config/fcm.php index a2ea487ea..e753e16c5 100644 --- a/config/fcm.php +++ b/config/fcm.php @@ -9,6 +9,7 @@ 'sender_id' => env('FCM_SENDER_ID', 'Your sender id'), 'server_send_url' => 'https://fcm.googleapis.com/fcm/send', 'server_group_url' => 'https://android.googleapis.com/gcm/notification', + 'server_topic_url' => 'https://iid.googleapis.com/iid/v1/', 'timeout' => 30.0, // in second ], ]; diff --git a/src/FCMServiceProvider.php b/src/FCMServiceProvider.php index 069622121..d6b43e5a7 100644 --- a/src/FCMServiceProvider.php +++ b/src/FCMServiceProvider.php @@ -4,8 +4,10 @@ use Illuminate\Support\Str; use LaravelFCM\Sender\FCMGroup; +use LaravelFCM\Sender\FCMTopic; use LaravelFCM\Sender\FCMSender; use Illuminate\Support\ServiceProvider; +use LaravelFCM\Validator\FCMValidator; use Monolog\Logger; use Monolog\Handler\StreamHandler; use Monolog\Handler\NullHandler; @@ -53,6 +55,14 @@ public function register() return new FCMGroup($client, $url, $logger); }); + $this->app->bind('fcm.topic', function ($app) { + $client = $app[ 'fcm.client' ]; + $url = $app[ 'config' ]->get('fcm.http.server_topic_url'); + $logger = $app[ 'fcm.logger' ]; + + return new FCMTopic($client, $url,$logger); + }); + $this->app->bind('fcm.sender', function ($app) { $client = $app[ 'fcm.client' ]; $url = $app[ 'config' ]->get('fcm.http.server_send_url'); @@ -60,10 +70,19 @@ public function register() return new FCMSender($client, $url, $logger); }); + + $this->app->bind('fcm.validator',function ($app){ + $client = $app[ 'fcm.client' ]; + $url = $app[ 'config' ]->get('fcm.http.server_send_url'); + $logger = $app[ 'fcm.logger' ]; + + + return new FCMValidator($client, $url, $logger); + }); } public function provides() { - return ['fcm.client', 'fcm.group', 'fcm.sender', 'fcm.logger']; + return ['fcm.client', 'fcm.group', 'fcm.sender', 'fcm.logger', 'fcm.topic' , 'fcm.validator']; } } diff --git a/src/Facades/FCMTopic.php b/src/Facades/FCMTopic.php new file mode 100644 index 000000000..363f7b002 --- /dev/null +++ b/src/Facades/FCMTopic.php @@ -0,0 +1,17 @@ +topic_id = $topic_id; + $this->recipients_tokens = $recipients_tokens; + $this->operation = $operation; + } + + /** + * Build the header for the request. + * + * @return array + */ + protected function buildBody() + { + if ($this->operation === 'create') { + return []; + } + + return [ + 'to' => '/topics/' . $this->topic_id, + 'registration_tokens' => $this->recipients_tokens, + ]; + } +} diff --git a/src/Request/ValidateRequest.php b/src/Request/ValidateRequest.php new file mode 100644 index 000000000..a2dfd52b4 --- /dev/null +++ b/src/Request/ValidateRequest.php @@ -0,0 +1,15 @@ +client->request('post', $this->url . $registrationId . '/rel/topics/' . $topicId, $request->build()); + + + if ($this->isValidResponse($response)) { + return true; + } + return false; + + } + + /** + * Add subscription to a topic. + * + * @param string $topicId + * @param array|string $recipientsTokens + * @return bool + */ + public function subscribeTopic($topicId, $recipientsTokens) + { + $request = new TopicRequest('subscribe', $topicId, $recipientsTokens); + $response = $this->client->request('post', $this->add_subscription_url, $request->build()); + + if ($this->isValidResponse($response)) { + return true; + } + return false; + } + + /** + * Remove subscription from a topic. + * + * + * @param string $topicId + * @param array|string $recipientsTokens + * @return bool + */ + public function unsubscribeTopic($topicId, $recipientsTokens) + { + $request = new TopicRequest('unsubscribe', $topicId, $recipientsTokens); + $response = $this->client->request('post', $this->remove_subscription_url, $request->build()); + + if ($this->isValidResponse($response)) { + return true; + } + return false; + } + + /** + * @param \Psr\Http\Message\ResponseInterface $response + * + * @return bool + */ + public function isValidResponse(ResponseInterface $response) + { + return $response->getStatusCode() === 200; + } +} diff --git a/src/Validator/FCMValidator.php b/src/Validator/FCMValidator.php new file mode 100644 index 000000000..6c3710a02 --- /dev/null +++ b/src/Validator/FCMValidator.php @@ -0,0 +1,32 @@ +build(); + if (isset($build['json'])) { + unset($build['json']); + } + $this->client->request('get', $this->validate_token_url . $token, $build); + return true; + }catch (Exception $e) { + return false; + } + } +}