diff --git a/README.md b/README.md index a819960c4..e992bd372 100644 --- a/README.md +++ b/README.md @@ -258,6 +258,35 @@ $topicResponse->shouldRetry(); $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 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 63cc65151..98ccc7239 100644 --- a/src/FCMServiceProvider.php +++ b/src/FCMServiceProvider.php @@ -3,6 +3,7 @@ namespace LaravelFCM; use LaravelFCM\Sender\FCMGroup; +use LaravelFCM\Sender\FCMTopic; use LaravelFCM\Sender\FCMSender; use Illuminate\Support\ServiceProvider; @@ -38,6 +39,13 @@ public function register() return new FCMGroup($client, $url); }); + $this->app->bind('fcm.topic', function ($app) { + $client = $app[ 'fcm.client' ]; + $url = $app[ 'config' ]->get('fcm.http.server_topic_url'); + + return new FCMTopic($client, $url); + }); + $this->app->bind('fcm.sender', function ($app) { $client = $app[ 'fcm.client' ]; $url = $app[ 'config' ]->get('fcm.http.server_send_url'); @@ -48,6 +56,6 @@ public function register() public function provides() { - return ['fcm.client', 'fcm.group', 'fcm.sender']; + return ['fcm.client', 'fcm.group', 'fcm.sender', 'fcm.topic']; } } diff --git a/src/Facades/FCMTopic.php b/src/Facades/FCMTopic.php new file mode 100644 index 000000000..71408bbdb --- /dev/null +++ b/src/Facades/FCMTopic.php @@ -0,0 +1,13 @@ +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/Sender/FCMTopic.php b/src/Sender/FCMTopic.php new file mode 100644 index 000000000..61bbb2679 --- /dev/null +++ b/src/Sender/FCMTopic.php @@ -0,0 +1,85 @@ +client->request('post', $this->url.$registration_id."/rel/topics/".$topic_id, $request->build()); + + if($this->isValidResponse($response)){ + return true; + } + return false; + } + + /** + * add subscription to a topic. + * + * @param $topic_id + * @param $recipients_tokens + * @return null|string notification_key + */ + public function subscribeTopic($topic_id, $recipients_tokens) + { + $request = new TopicRequest('subscribe', $topic_id, $recipients_tokens); + $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 $topic_id + * @param $recipients_tokens + * @return null|string notification_key + */ + public function unsubscribeTopic($topic_id, $recipients_tokens) + { + $request = new TopicRequest('unsubscribe', $topic_id, $recipients_tokens); + $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; + } +}