|
1 | 1 | <?php |
2 | | -namespace sngrl\PhpFirebaseCloudMessaging; |
| 2 | + |
| 3 | +namespace RedjanYm\FCM; |
3 | 4 |
|
4 | 5 | use GuzzleHttp; |
| 6 | +use Psr\Http\Message\ResponseInterface; |
5 | 7 |
|
6 | | -/** |
7 | | - * @author sngrl |
8 | | - */ |
9 | 8 | class Client implements ClientInterface |
10 | 9 | { |
11 | | - const DEFAULT_API_URL = 'https://fcm.googleapis.com/fcm/send'; |
12 | | - const DEFAULT_TOPIC_ADD_SUBSCRIPTION_API_URL = 'https://iid.googleapis.com/iid/v1:batchAdd'; |
13 | | - const DEFAULT_TOPIC_REMOVE_SUBSCRIPTION_API_URL = 'https://iid.googleapis.com/iid/v1:batchRemove'; |
14 | | - |
15 | | - private $apiKey; |
16 | | - private $proxyApiUrl; |
17 | | - private $guzzleClient; |
| 10 | + const DEFAULT_API_URL = 'https://fcm.googleapis.com/v1/projects/{PROJECT_ID}/messages:send'; |
| 11 | + private string $projectId; |
| 12 | + private string $serviceAccountPath; |
| 13 | + private string $accessToken; |
| 14 | + private GuzzleHttp\Client $guzzleClient; |
18 | 15 |
|
19 | | - public function __construct() |
| 16 | + public function __construct(string $serviceAccountPath) |
20 | 17 | { |
| 18 | + if (file_exists($serviceAccountPath) === false) { |
| 19 | + throw new \InvalidArgumentException('Service account file does not exist!'); |
| 20 | + } |
21 | 21 |
|
| 22 | + $this->serviceAccountPath = $serviceAccountPath; |
22 | 23 | $this->guzzleClient = new \GuzzleHttp\Client(); |
| 24 | + $this->applyCredentials(); |
23 | 25 | } |
24 | 26 |
|
25 | | - /** |
26 | | - * add your server api key here |
27 | | - * read how to obtain an api key here: https://firebase.google.com/docs/server/setup#prerequisites |
28 | | - * |
29 | | - * @param string $apiKey |
30 | | - * |
31 | | - * @return \sngrl\PhpFirebaseCloudMessaging\Client |
32 | | - */ |
33 | | - public function setApiKey($apiKey) |
| 27 | + public function setServiceAccountPath(string $serviceAccountPath): self |
34 | 28 | { |
35 | | - $this->apiKey = $apiKey; |
36 | | - return $this; |
37 | | - } |
| 29 | + if (file_exists($serviceAccountPath) === false) { |
| 30 | + throw new \InvalidArgumentException('Service account file does not exist!'); |
| 31 | + } |
38 | 32 |
|
39 | | - /** |
40 | | - * people can overwrite the api url with a proxy server url of their own |
41 | | - * |
42 | | - * @param string $url |
43 | | - * |
44 | | - * @return \sngrl\PhpFirebaseCloudMessaging\Client |
45 | | - */ |
46 | | - public function setProxyApiUrl($url) |
47 | | - { |
48 | | - $this->proxyApiUrl = $url; |
| 33 | + $this->serviceAccountPath = $serviceAccountPath; |
| 34 | + $this->applyCredentials(); |
49 | 35 | return $this; |
50 | 36 | } |
51 | 37 |
|
52 | | - /** |
53 | | - * sends your notification to the google servers and returns a guzzle repsonse object |
54 | | - * containing their answer. |
55 | | - * |
56 | | - * @param Message $message |
57 | | - * |
58 | | - * @return \Psr\Http\Message\ResponseInterface |
59 | | - * @throws \GuzzleHttp\Exception\RequestException |
60 | | - */ |
61 | | - public function send(Message $message) |
| 38 | + private function applyCredentials(): self |
62 | 39 | { |
63 | | - return $this->guzzleClient->post( |
64 | | - $this->getApiUrl(), |
65 | | - [ |
66 | | - 'headers' => [ |
67 | | - 'Authorization' => sprintf('key=%s', $this->apiKey), |
68 | | - 'Content-Type' => 'application/json' |
69 | | - ], |
70 | | - 'body' => json_encode($message) |
71 | | - ] |
72 | | - ); |
73 | | - } |
| 40 | + $this->projectId = \json_decode(file_get_contents($this->serviceAccountPath), true)['project_id']; |
| 41 | + $this->accessToken = $this->getAccessToken(); |
74 | 42 |
|
75 | | - /** |
76 | | - * @param integer $topic_id |
77 | | - * @param array|string $recipients_tokens |
78 | | - * |
79 | | - * @return \Psr\Http\Message\ResponseInterface |
80 | | - */ |
81 | | - public function addTopicSubscription($topic_id, $recipients_tokens) |
82 | | - { |
83 | | - return $this->processTopicSubscription($topic_id, $recipients_tokens, self::DEFAULT_TOPIC_ADD_SUBSCRIPTION_API_URL); |
| 43 | + return $this; |
84 | 44 | } |
85 | 45 |
|
86 | | - |
87 | | - /** |
88 | | - * @param integer $topic_id |
89 | | - * @param array|string $recipients_tokens |
90 | | - * |
91 | | - * @return \Psr\Http\Message\ResponseInterface |
92 | | - */ |
93 | | - public function removeTopicSubscription($topic_id, $recipients_tokens) |
| 46 | + private function getAccessToken(): string |
94 | 47 | { |
95 | | - return $this->processTopicSubscription($topic_id, $recipients_tokens, self::DEFAULT_TOPIC_REMOVE_SUBSCRIPTION_API_URL); |
96 | | - } |
| 48 | + $client = new \Google\Client(); |
| 49 | + $client->setAuthConfig($this->serviceAccountPath); |
| 50 | + $client->addScope('https://www.googleapis.com/auth/firebase.messaging'); |
| 51 | + $client->useApplicationDefaultCredentials(); |
| 52 | + $token = $client->fetchAccessTokenWithAssertion(); |
97 | 53 |
|
| 54 | + return $token['access_token']; |
| 55 | + } |
98 | 56 |
|
99 | 57 | /** |
100 | | - * @param integer $topic_id |
101 | | - * @param array|string $recipients_tokens |
102 | | - * @param string $url |
103 | | - * |
104 | | - * @return \Psr\Http\Message\ResponseInterface |
| 58 | + * @throws \GuzzleHttp\Exception\RequestException |
105 | 59 | */ |
106 | | - protected function processTopicSubscription($topic_id, $recipients_tokens, $url) |
| 60 | + public function send(Notification $message): ResponseInterface |
107 | 61 | { |
108 | | - if (!is_array($recipients_tokens)) |
109 | | - $recipients_tokens = [$recipients_tokens]; |
110 | | - |
111 | 62 | return $this->guzzleClient->post( |
112 | | - $url, |
| 63 | + $this->getApiUrl(), |
113 | 64 | [ |
114 | 65 | 'headers' => [ |
115 | | - 'Authorization' => sprintf('key=%s', $this->apiKey), |
| 66 | + 'Authorization' => sprintf('Bearer %s', $this->accessToken), |
116 | 67 | 'Content-Type' => 'application/json' |
117 | 68 | ], |
118 | | - 'body' => json_encode([ |
119 | | - 'to' => '/topics/' . $topic_id, |
120 | | - 'registration_tokens' => $recipients_tokens, |
| 69 | + 'body' => \json_encode([ |
| 70 | + 'message' => $message, |
121 | 71 | ]) |
122 | 72 | ] |
123 | 73 | ); |
124 | 74 | } |
125 | 75 |
|
126 | 76 | private function getApiUrl() |
127 | 77 | { |
128 | | - return isset($this->proxyApiUrl) ? $this->proxyApiUrl : self::DEFAULT_API_URL; |
| 78 | + return str_replace('{PROJECT_ID}', $this->projectId, self::DEFAULT_API_URL); |
129 | 79 | } |
130 | 80 | } |
0 commit comments