1 | <?php |
||
17 | class Message |
||
18 | { |
||
19 | use ParameterTrait { |
||
20 | ParameterTrait::hasParameter as hasDefinedParameter; |
||
21 | ParameterTrait::getParameter as getDefinedParameter; |
||
22 | ParameterTrait::setParameter as setDefinedParameter; |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Valid message parameters. |
||
27 | * |
||
28 | * @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG |
||
29 | * /Chapters/TheNotificationPayload.html |
||
30 | * @see https://developers.google.com/cloud-messaging/http-server-ref#downstream-http-messages-json |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected static $validParameters = [ |
||
35 | // Common |
||
36 | 'title', |
||
37 | 'body', |
||
38 | |||
39 | // Common mapped |
||
40 | 'title_loc_key', |
||
41 | 'title_loc_args', |
||
42 | 'body_loc_key', |
||
43 | 'body_loc_args', |
||
44 | |||
45 | // APNS |
||
46 | 'action-loc-key', |
||
47 | 'launch-image', |
||
48 | |||
49 | // GCM |
||
50 | 'icon', |
||
51 | 'sound', |
||
52 | 'tag', |
||
53 | 'color', |
||
54 | 'click_action', |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * Parameter key map |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected static $parameterMap = [ |
||
63 | 'title-loc-key' => 'title_loc_key', |
||
64 | 'title-loc-args' => 'title_loc_args', |
||
65 | 'loc-key' => 'body_loc_key', |
||
66 | 'loc-args' => 'body_loc_args', |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * Reserved payload keys. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected static $reservedPayloadRegex = [ |
||
75 | // APNS |
||
76 | '/^apc$/', |
||
77 | |||
78 | // GCM |
||
79 | '/^(google|gcm)/', |
||
80 | '/^from$/', |
||
81 | '/^collapse_key$/', |
||
82 | '/^delay_while_idle$/', |
||
83 | '/^time_to_live$/', |
||
84 | '/^restricted_package_name$/', |
||
85 | '/^dry_run$/', |
||
86 | '/^priority$/', |
||
87 | '/^content_available$/', |
||
88 | ]; |
||
89 | |||
90 | /** |
||
91 | * Payload prefix |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | protected $payloadPrefix = 'data_'; |
||
96 | |||
97 | /** |
||
98 | * Message payload. |
||
99 | * |
||
100 | * @var \Doctrine\Common\Collections\ArrayCollection |
||
101 | */ |
||
102 | protected $payload; |
||
103 | |||
104 | /** |
||
105 | * Constructor. |
||
106 | * |
||
107 | * @param array $parameters |
||
108 | */ |
||
109 | public function __construct(array $parameters = []) |
||
115 | |||
116 | /** |
||
117 | * Convenience method to set message title. |
||
118 | * |
||
119 | * @param string $title |
||
120 | * |
||
121 | * @return $this |
||
122 | */ |
||
123 | public function setTitle($title) |
||
129 | |||
130 | /** |
||
131 | * Convenience method to set message body. |
||
132 | * |
||
133 | * @param string $body |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function setBody($body) |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function hasParameter($parameter) |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | public function getParameter($parameter, $default = null) |
||
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | * |
||
163 | * @throws \InvalidArgumentException |
||
164 | */ |
||
165 | public function setParameter($parameter, $value) |
||
175 | |||
176 | private function getMappedParameter($parameter) |
||
184 | |||
185 | /** |
||
186 | * Retrieve payload prefix. |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getPayloadPrefix() |
||
194 | |||
195 | /** |
||
196 | * Set payload prefix. |
||
197 | * |
||
198 | * @param string $prefix |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function setPayloadPrefix($prefix) |
||
208 | |||
209 | /** |
||
210 | * Get payload data. |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | public function getPayloadData() |
||
218 | |||
219 | /** |
||
220 | * Set payload data. |
||
221 | * |
||
222 | * @param array $data |
||
223 | * |
||
224 | * @throws \InvalidArgumentException |
||
225 | * |
||
226 | * @return $this |
||
227 | */ |
||
228 | public function setPayloadData(array $data) |
||
238 | |||
239 | /** |
||
240 | * Has payload data. |
||
241 | * |
||
242 | * @param string $key |
||
243 | * |
||
244 | * @throws \InvalidArgumentException |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | public function hasPayload($key) |
||
252 | |||
253 | /** |
||
254 | * Get payload data. |
||
255 | * |
||
256 | * @param string $key |
||
257 | * @param mixed $default |
||
258 | * |
||
259 | * @throws \InvalidArgumentException |
||
260 | * |
||
261 | * @return mixed |
||
262 | */ |
||
263 | public function getPayload($key, $default = null) |
||
269 | |||
270 | /** |
||
271 | * Set payload data. |
||
272 | * |
||
273 | * @param string $key |
||
274 | * @param mixed $value |
||
275 | * |
||
276 | * @throws \InvalidArgumentException |
||
277 | * |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function setPayload($key, $value) |
||
298 | |||
299 | /** |
||
300 | * Compose payload key with prefix. |
||
301 | * |
||
302 | * @param string $key |
||
303 | * |
||
304 | * @throws \InvalidArgumentException |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | protected function composePayloadKey($key) |
||
318 | } |
||
319 |