1 | <?php |
||
14 | class Fcm |
||
15 | { |
||
16 | |||
17 | use InstanceConfigTrait; |
||
18 | |||
19 | /** |
||
20 | * Array for devices's token |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $tokens = []; |
||
25 | |||
26 | /** |
||
27 | * Array for the notification |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $notification = []; |
||
32 | |||
33 | /** |
||
34 | * Array of datas |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $datas = []; |
||
39 | |||
40 | /** |
||
41 | * Array of request parameters |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $parameters = []; |
||
46 | |||
47 | /** |
||
48 | * Array of payload |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $payload = []; |
||
53 | |||
54 | /** |
||
55 | * Response of the request |
||
56 | * |
||
57 | * @var \Cake\Http\Client\Response |
||
58 | */ |
||
59 | protected $response; |
||
60 | |||
61 | /** |
||
62 | * Default config |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $_defaultConfig = [ |
||
67 | 'parameters' => [ |
||
68 | 'collapse_key' => null, |
||
69 | 'priority' => 'normal', |
||
70 | 'dry_run' => false, |
||
71 | 'time_to_live' => 0, |
||
72 | 'restricted_package_name' => null |
||
73 | ], |
||
74 | 'http' => [] |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * List of keys allowed to be used in notification array. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $_allowedNotificationKeys = [ |
||
83 | 'title', |
||
84 | 'body', |
||
85 | 'icon', |
||
86 | 'sound', |
||
87 | 'badge', |
||
88 | 'tag', |
||
89 | 'color', |
||
90 | 'click_action', |
||
91 | 'body_loc_key', |
||
92 | 'body_loc_args', |
||
93 | 'title_loc_key', |
||
94 | 'title_loc_args', |
||
95 | ]; |
||
96 | |||
97 | /** |
||
98 | * FcmAdapter constructor. |
||
99 | * |
||
100 | * @throws \ker0x\Push\Adapter\Exception\InvalidAdapterException |
||
101 | */ |
||
102 | public function __construct() |
||
111 | |||
112 | /** |
||
113 | * Getter for tokens |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | public function getTokens() |
||
121 | |||
122 | /** |
||
123 | * Setter for tokens |
||
124 | * |
||
125 | * @param array $tokens Array of devices's token |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function setTokens(array $tokens) |
||
135 | |||
136 | /** |
||
137 | * Getter for notification |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | public function getNotification() |
||
145 | |||
146 | /** |
||
147 | * Setter for notification |
||
148 | * |
||
149 | * @param array $notification Array of keys for the notification |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function setNotification(array $notification) |
||
162 | |||
163 | /** |
||
164 | * Getter for datas |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | public function getDatas() |
||
172 | |||
173 | /** |
||
174 | * Setter for datas |
||
175 | * |
||
176 | * @param array $datas Array of datas for the push |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function setDatas(array $datas) |
||
192 | |||
193 | /** |
||
194 | * Getter for parameters |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | public function getParameters() |
||
202 | |||
203 | /** |
||
204 | * Setter for parameters |
||
205 | * |
||
206 | * @param array $parameters Array of parameters for the push |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function setParameters(array $parameters) |
||
216 | |||
217 | /** |
||
218 | * Getter for payload |
||
219 | * |
||
220 | * @return array |
||
221 | */ |
||
222 | public function getPayload() |
||
236 | |||
237 | /** |
||
238 | * Check tokens's array |
||
239 | * |
||
240 | * @param array $tokens Token's array |
||
241 | * @return void |
||
242 | * @throws \ker0x\Push\Adapter\Fcm\Exception\InvalidTokenException |
||
243 | */ |
||
244 | private function _checkTokens($tokens) |
||
250 | |||
251 | /** |
||
252 | * Check notification's array |
||
253 | * |
||
254 | * @param array $notification Notification's array |
||
255 | * @return void |
||
256 | * @throws \ker0x\Push\Adapter\Fcm\Exception\InvalidNotificationException |
||
257 | */ |
||
258 | private function _checkNotification($notification) |
||
276 | |||
277 | /** |
||
278 | * Check datas's array |
||
279 | * |
||
280 | * @param array $datas Datas's array |
||
281 | * @return void |
||
282 | * @throws \ker0x\Push\Adapter\Fcm\Exception\InvalidDataException |
||
283 | */ |
||
284 | private function _checkDatas($datas) |
||
290 | |||
291 | /** |
||
292 | * Check parameters's array |
||
293 | * |
||
294 | * @param array $parameters Parameters's array |
||
295 | * @return void |
||
296 | * @throws \ker0x\Push\Adapter\Fcm\Exception\InvalidParametersException |
||
297 | */ |
||
298 | private function _checkParameters($parameters) |
||
304 | |||
305 | /** |
||
306 | * Execute the push |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | protected function _executePush() |
||
320 | |||
321 | /** |
||
322 | * Build the message |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | private function _buildMessage() |
||
343 | |||
344 | /** |
||
345 | * Return options for the HTTP request |
||
346 | * |
||
347 | * @return array $options |
||
348 | */ |
||
349 | private function _getHttpOptions() |
||
361 | } |
||
362 |