1 | <?php |
||
13 | class Fcm |
||
14 | { |
||
15 | |||
16 | use InstanceConfigTrait; |
||
17 | |||
18 | /** |
||
19 | * Array for devices's token |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $tokens = []; |
||
24 | |||
25 | /** |
||
26 | * Array for the notification |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $notification = []; |
||
31 | |||
32 | /** |
||
33 | * Array of data |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $data = []; |
||
38 | |||
39 | /** |
||
40 | * Array of request options |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $options = []; |
||
45 | |||
46 | /** |
||
47 | * Array of payload |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $payload = []; |
||
52 | |||
53 | /** |
||
54 | * Response of the request |
||
55 | * |
||
56 | * @var \Cake\Http\Client\Response |
||
57 | */ |
||
58 | protected $response; |
||
59 | |||
60 | /** |
||
61 | * Default config |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $_defaultConfig = [ |
||
66 | 'options' => [ |
||
67 | 'collapse_key' => null, |
||
68 | 'priority' => 'normal', |
||
69 | 'dry_run' => false, |
||
70 | 'time_to_live' => 0, |
||
71 | 'restricted_package_name' => null, |
||
72 | ], |
||
73 | 'http' => [], |
||
74 | ]; |
||
75 | |||
76 | /** |
||
77 | * FcmAdapter constructor. |
||
78 | * |
||
79 | * @throws \ker0x\Push\Adapter\InvalidAdapterException |
||
80 | */ |
||
81 | public function __construct() |
||
90 | |||
91 | /** |
||
92 | * Getter for tokens. |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | public function getTokens() |
||
100 | |||
101 | /** |
||
102 | * Setter for tokens. |
||
103 | * |
||
104 | * @param array $tokens Array of tokens. |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function setTokens(array $tokens) |
||
114 | |||
115 | /** |
||
116 | * Getter for payload notification. |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | public function getNotification() |
||
124 | |||
125 | /** |
||
126 | * Setter for payload notification. |
||
127 | * |
||
128 | * Authorized keys for the notification are: |
||
129 | * |
||
130 | * - `title` Indicates notification title. |
||
131 | * - `body` Indicates notification body text. |
||
132 | * - `badge` Indicates the badge on the client app home icon. (iOS) |
||
133 | * - `icon` Indicates notification icon. (Android) |
||
134 | * - `sound` Indicates a sound to play when the device receives a notification. |
||
135 | * - `tag` Indicates whether each notification results in a new entry in the |
||
136 | * notification drawer on Android. (Android) |
||
137 | * - `color` Indicates color of the icon, expressed in #rrggbb format. (Android) |
||
138 | * - `click_action` Indicates the action associated with a user click on the notification. |
||
139 | * - `body_loc_key` Indicates the key to the body string for localization. |
||
140 | * - `body_loc_args` Indicates the string value to replace format specifiers in the |
||
141 | * body string for localization. |
||
142 | * - `title_loc_key` Indicates the key to the title string for localization. |
||
143 | * - `title_loc_args` Indicates the string value to replace format specifiers in |
||
144 | * the title string for localization. |
||
145 | * |
||
146 | * @param array|\ker0x\Push\Adapter\Fcm\Message\NotificationBuilder $notification Array of keys for the notification |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function setNotification($notification) |
||
155 | |||
156 | /** |
||
157 | * Getter for payload data. |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | public function getData() |
||
165 | |||
166 | /** |
||
167 | * Setter for payload data. |
||
168 | * |
||
169 | * @param array $data Array of data for the push. |
||
170 | * @return $this |
||
171 | */ |
||
172 | public function setData($data) |
||
178 | |||
179 | /** |
||
180 | * Getter for payload options |
||
181 | * |
||
182 | * @return array |
||
183 | */ |
||
184 | public function getOptions() |
||
188 | |||
189 | /** |
||
190 | * Setter for payload options. |
||
191 | * |
||
192 | * Authorized keys for options's array are: |
||
193 | * |
||
194 | * - `collapse_key` This parameter identifies a group of messages. |
||
195 | * - `priority` Sets the priority of the message. |
||
196 | * - `content_available` When a notification or message is sent and |
||
197 | * this is set to true, an inactive client app is awoken. |
||
198 | * - `time_to_live` This parameter specifies how long (in seconds) |
||
199 | * the message should be kept in FCM storage if the device is offline. |
||
200 | * - `restricted_package_name` This parameter specifies the package name |
||
201 | * of the application where the registration tokens must match in order |
||
202 | * to receive the message. |
||
203 | * - `dry_run` This parameter, when set to true, allows developers to test |
||
204 | * a request without actually sending a message. |
||
205 | * |
||
206 | * @param array|\ker0x\Push\Adapter\Fcm\Message\OptionsBuilder $options Options for the push |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function setOptions($options) |
||
215 | |||
216 | /** |
||
217 | * Getter for payload. |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | public function getPayload() |
||
235 | |||
236 | /** |
||
237 | * Check tokens's array. |
||
238 | * |
||
239 | * @param array $tokens An array of tokens. |
||
240 | * @return void |
||
241 | * @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidTokenException |
||
242 | */ |
||
243 | private function _checkTokens($tokens) |
||
249 | |||
250 | /** |
||
251 | * Build the message. |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | protected function _getMessage() |
||
272 | |||
273 | /** |
||
274 | * Return options for the HTTP request. |
||
275 | * |
||
276 | * @return array $options |
||
277 | */ |
||
278 | protected function _getHttpOptions() |
||
290 | } |
||
291 |