|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LaravelFCM\Sender; |
| 4 | + |
| 5 | +use LaravelFCM\Request\TopicRequest; |
| 6 | +use Psr\Http\Message\ResponseInterface; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class FCMGroup. |
| 10 | + */ |
| 11 | +class FCMTopic extends HTTPSender { |
| 12 | + |
| 13 | + private $add_subscription_url = 'https://iid.googleapis.com/iid/v1:batchAdd'; |
| 14 | + private $remove_subscription_url = 'https://iid.googleapis.com/iid/v1:batchRemove'; |
| 15 | + |
| 16 | + /** |
| 17 | + * Create a topic. |
| 18 | + * |
| 19 | + * @param string $topicId |
| 20 | + * @param string $registrationId |
| 21 | + * |
| 22 | + * @return bool |
| 23 | + */ |
| 24 | + public function createTopic($topicId, $registrationId) |
| 25 | + { |
| 26 | + $request = new TopicRequest('create', $topicId); |
| 27 | + if (is_array($registrationId)) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + $response = $this->client->request('post', $this->url . $registrationId . '/rel/topics/' . $topicId, $request->build()); |
| 31 | + |
| 32 | + |
| 33 | + if ($this->isValidResponse($response)) { |
| 34 | + return true; |
| 35 | + } |
| 36 | + return false; |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Add subscription to a topic. |
| 42 | + * |
| 43 | + * @param string $topicId |
| 44 | + * @param array|string $recipientsTokens |
| 45 | + * @return bool |
| 46 | + */ |
| 47 | + public function subscribeTopic($topicId, $recipientsTokens) |
| 48 | + { |
| 49 | + $request = new TopicRequest('subscribe', $topicId, $recipientsTokens); |
| 50 | + $response = $this->client->request('post', $this->add_subscription_url, $request->build()); |
| 51 | + |
| 52 | + if ($this->isValidResponse($response)) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Remove subscription from a topic. |
| 60 | + * |
| 61 | + * |
| 62 | + * @param string $topicId |
| 63 | + * @param array|string $recipientsTokens |
| 64 | + * @return bool |
| 65 | + */ |
| 66 | + public function unsubscribeTopic($topicId, $recipientsTokens) |
| 67 | + { |
| 68 | + $request = new TopicRequest('unsubscribe', $topicId, $recipientsTokens); |
| 69 | + $response = $this->client->request('post', $this->remove_subscription_url, $request->build()); |
| 70 | + |
| 71 | + if ($this->isValidResponse($response)) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param \Psr\Http\Message\ResponseInterface $response |
| 79 | + * |
| 80 | + * @return bool |
| 81 | + */ |
| 82 | + public function isValidResponse(ResponseInterface $response) |
| 83 | + { |
| 84 | + return $response->getStatusCode() === 200; |
| 85 | + } |
| 86 | +} |
0 commit comments