1 | <?php |
||
10 | class Fcm extends AbstractAdapter |
||
11 | { |
||
12 | |||
13 | const PRIORITY_NORMAL = 'normal'; |
||
14 | const PRIORITY_HIGH = 'high'; |
||
15 | |||
16 | /** |
||
17 | * Array for devices's token |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $tokens = []; |
||
22 | |||
23 | /** |
||
24 | * Array for the notification |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $notification = []; |
||
29 | |||
30 | /** |
||
31 | * Array of datas |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $datas = []; |
||
36 | |||
37 | /** |
||
38 | * Array of request parameters |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $parameters = []; |
||
43 | |||
44 | /** |
||
45 | * Array of payload |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $payload = []; |
||
50 | |||
51 | /** |
||
52 | * Default config |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $_defaultConfig = [ |
||
57 | 'parameters' => [ |
||
58 | 'collapse_key' => null, |
||
59 | 'priority' => self::PRIORITY_NORMAL, |
||
60 | 'content_available' => false, |
||
61 | 'mutable_content' => false, |
||
62 | 'time_to_live' => 0, |
||
63 | 'restricted_package_name' => null, |
||
64 | 'dry_run' => false, |
||
65 | ], |
||
66 | 'http' => [], |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * List of keys allowed to be used in notification array. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $_allowedNotificationKeys = [ |
||
75 | 'title', |
||
76 | 'body', |
||
77 | 'icon', |
||
78 | 'sound', |
||
79 | 'badge', |
||
80 | 'tag', |
||
81 | 'color', |
||
82 | 'click_action', |
||
83 | 'body_loc_key', |
||
84 | 'body_loc_args', |
||
85 | 'title_loc_key', |
||
86 | 'title_loc_args', |
||
87 | ]; |
||
88 | |||
89 | /** |
||
90 | * FcmAdapter constructor. |
||
91 | * |
||
92 | * @throws \Exception |
||
93 | */ |
||
94 | public function __construct() |
||
100 | |||
101 | /** |
||
102 | * Getter for tokens |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | public function getTokens() |
||
110 | |||
111 | /** |
||
112 | * Setter for tokens |
||
113 | * |
||
114 | * @param array $tokens Array of devices's token |
||
115 | * @return $this |
||
116 | */ |
||
117 | public function setTokens(array $tokens) |
||
124 | |||
125 | /** |
||
126 | * Getter for notification |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | public function getNotification() |
||
134 | |||
135 | /** |
||
136 | * Setter for notification |
||
137 | * |
||
138 | * @param array $notification Array of keys for the notification |
||
139 | * @return $this |
||
140 | */ |
||
141 | public function setNotification(array $notification) |
||
151 | |||
152 | /** |
||
153 | * Getter for datas |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | public function getDatas() |
||
161 | |||
162 | /** |
||
163 | * Setter for datas |
||
164 | * |
||
165 | * @param array $datas Array of datas for the push |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function setDatas(array $datas) |
||
181 | |||
182 | /** |
||
183 | * Getter for parameters |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | public function getParameters() |
||
191 | |||
192 | /** |
||
193 | * Setter for parameters |
||
194 | * |
||
195 | * @param array $parameters Array of parameters for the push |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function setParameters(array $parameters) |
||
205 | |||
206 | /** |
||
207 | * Getter for payload |
||
208 | * |
||
209 | * @return array |
||
210 | */ |
||
211 | public function getPayload() |
||
225 | |||
226 | /** |
||
227 | * Execute the push |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function send() |
||
241 | |||
242 | /** |
||
243 | * Check tokens's array |
||
244 | * |
||
245 | * @param array $tokens Token's array |
||
246 | * @return void |
||
247 | * @throws \InvalidArgumentException |
||
248 | */ |
||
249 | private function _checkTokens(array $tokens) |
||
255 | |||
256 | /** |
||
257 | * Check notification's array |
||
258 | * |
||
259 | * @param array $notification Notification's array |
||
260 | * @return void |
||
261 | * @throws \InvalidArgumentException |
||
262 | */ |
||
263 | private function _checkNotification(array $notification) |
||
281 | |||
282 | /** |
||
283 | * Check datas's array |
||
284 | * |
||
285 | * @param array $datas Datas's array |
||
286 | * @return void |
||
287 | * @throws \InvalidArgumentException |
||
288 | */ |
||
289 | private function _checkDatas(array $datas) |
||
295 | |||
296 | /** |
||
297 | * Check parameters's array |
||
298 | * |
||
299 | * @param array $parameters Parameters's array |
||
300 | * @return void |
||
301 | * @throws \InvalidArgumentException |
||
302 | */ |
||
303 | private function _checkParameters(array $parameters) |
||
309 | |||
310 | /** |
||
311 | * Build the message |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | private function _buildMessage() |
||
332 | |||
333 | /** |
||
334 | * Return options for the HTTP request |
||
335 | * |
||
336 | * @return array $options |
||
337 | */ |
||
338 | private function _getHttpOptions() |
||
350 | } |
||
351 |