1 | <?php |
||
15 | class FcmAdapter implements AdapterInterface |
||
16 | { |
||
17 | |||
18 | use InstanceConfigTrait; |
||
19 | |||
20 | /** |
||
21 | * Array for devices's token |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $tokens = []; |
||
26 | |||
27 | /** |
||
28 | * Array for the notification |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $notification = []; |
||
33 | |||
34 | /** |
||
35 | * Array of datas |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $datas = []; |
||
40 | |||
41 | /** |
||
42 | * Array of request parameters |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $parameters = []; |
||
47 | |||
48 | /** |
||
49 | * Array of payload |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $payload = []; |
||
54 | |||
55 | /** |
||
56 | * Response of the request |
||
57 | * |
||
58 | * @var \Cake\Http\Client\Response |
||
59 | */ |
||
60 | protected $response; |
||
61 | |||
62 | /** |
||
63 | * Default config |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $_defaultConfig = [ |
||
68 | 'parameters' => [ |
||
69 | 'collapse_key' => null, |
||
70 | 'priority' => 'normal', |
||
71 | 'dry_run' => false, |
||
72 | 'time_to_live' => 0, |
||
73 | 'restricted_package_name' => null |
||
74 | ], |
||
75 | 'http' => [] |
||
76 | ]; |
||
77 | |||
78 | /** |
||
79 | * List of keys allowed to be used in notification array. |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $_allowedNotificationParameters = [ |
||
84 | 'title', |
||
85 | 'body', |
||
86 | 'icon', |
||
87 | 'sound', |
||
88 | 'badge', |
||
89 | 'tag', |
||
90 | 'color', |
||
91 | 'click_action', |
||
92 | 'body_loc_key', |
||
93 | 'body_loc_args', |
||
94 | 'title_loc_key', |
||
95 | 'title_loc_args', |
||
96 | ]; |
||
97 | |||
98 | /** |
||
99 | * FcmAdapter constructor. |
||
100 | * |
||
101 | * @throws \ker0x\Push\Exception\InvalidAdapterException |
||
102 | */ |
||
103 | public function __construct() |
||
112 | |||
113 | /** |
||
114 | * Send the request |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function send() |
||
122 | |||
123 | /** |
||
124 | * Display the response of the request |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | public function response() |
||
132 | |||
133 | /** |
||
134 | * Getter for tokens |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | public function getTokens(): array |
||
142 | |||
143 | /** |
||
144 | * Setter for tokens |
||
145 | * |
||
146 | * @param array $tokens |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function setTokens(array $tokens) |
||
156 | |||
157 | /** |
||
158 | * Getter for notification |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | public function getNotification(): array |
||
166 | |||
167 | /** |
||
168 | * Setter for notification |
||
169 | * |
||
170 | * @param array $notification |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function setNotification(array $notification) |
||
183 | |||
184 | /** |
||
185 | * Getter for datas |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | public function getDatas(): array |
||
193 | |||
194 | /** |
||
195 | * Setter for datas |
||
196 | * |
||
197 | * @param array $datas |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function setDatas(array $datas) |
||
213 | |||
214 | /** |
||
215 | * Getter for parameters |
||
216 | * |
||
217 | * @return array |
||
218 | */ |
||
219 | public function getParameters(): array |
||
223 | |||
224 | /** |
||
225 | * Setter for parameters |
||
226 | * |
||
227 | * @param array $parameters |
||
228 | * @return $this |
||
229 | */ |
||
230 | public function setParameters(array $parameters) |
||
237 | |||
238 | /** |
||
239 | * Getter for payload |
||
240 | * |
||
241 | * @return array |
||
242 | */ |
||
243 | public function getPayload(): array |
||
257 | |||
258 | /** |
||
259 | * Check tokens's array |
||
260 | * |
||
261 | * @param array $tokens |
||
262 | * @return void |
||
263 | * @throws \ker0x\Push\Exception\InvalidTokenException |
||
264 | */ |
||
265 | private function _checkTokens($tokens) |
||
271 | |||
272 | /** |
||
273 | * Check notification's array |
||
274 | * |
||
275 | * @param array $notification |
||
276 | * @return void |
||
277 | * @throws \ker0x\Push\Exception\InvalidNotificationException |
||
278 | */ |
||
279 | private function _checkNotification($notification) |
||
297 | |||
298 | /** |
||
299 | * Check datas's array |
||
300 | * |
||
301 | * @param array $datas |
||
302 | * @return void |
||
303 | * @throws \ker0x\Push\Exception\InvalidDataException |
||
304 | */ |
||
305 | private function _checkDatas($datas) |
||
311 | |||
312 | /** |
||
313 | * Check parameters's array |
||
314 | * |
||
315 | * @param array $parameters |
||
316 | * @return void |
||
317 | * @throws \ker0x\Push\Exception\InvalidParametersException |
||
318 | */ |
||
319 | private function _checkParameters($parameters) |
||
325 | |||
326 | /** |
||
327 | * Execute the push |
||
328 | * |
||
329 | * @return bool |
||
330 | */ |
||
331 | private function _executePush() |
||
341 | |||
342 | /** |
||
343 | * Build the message |
||
344 | * |
||
345 | * @return string |
||
346 | */ |
||
347 | private function _buildMessage() |
||
364 | |||
365 | /** |
||
366 | * Return options for the HTTP request |
||
367 | * |
||
368 | * @return array $options |
||
369 | */ |
||
370 | private function _getHttpOptions() |
||
382 | } |
||
383 |