· 6 years ago · Sep 19, 2019, 07:40 AM
1<?php
2namespace OneSignal;
3use Symfony\Component\OptionsResolver\Options;
4use Symfony\Component\OptionsResolver\OptionsResolver;
5class Notifications
6{
7const NOTIFICATIONS_LIMIT = 50;
8protected $api;
9public function __construct(OneSignal $api)
10{
11$this->api = $api;
12}
13/**
14* Get information about notification with provided ID.
15*
16* Application authentication key and ID must be set.
17*
18* @param string $id Notification ID
19*
20* @return array
21*/
22public function getOne($id)
23{
24$url = '/notifications/'.$id.'?app_id='.$this->api->getConfig()->getApplicationId();
25return $this->api->request('GET', $url, [
26'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
27]);
28}
29/**
30* Get information about all notifications.
31*
32* Application authentication key and ID must be set.
33*
34* @param int $limit How many notifications to return (max 50)
35* @param int $offset Results offset (results are sorted by ID)
36*
37* @return array
38*/
39public function getAll($limit = self::NOTIFICATIONS_LIMIT, $offset = 0)
40{
41$query = [
42'limit' => max(1, min(self::NOTIFICATIONS_LIMIT, filter_var($limit, FILTER_VALIDATE_INT))),
43'offset' => max(0, filter_var($offset, FILTER_VALIDATE_INT)),
44'app_id' => $this->api->getConfig()->getApplicationId(),
45];
46return $this->api->request('GET', '/notifications?'.http_build_query($query), [
47'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
48]);
49}
50/**
51* Send new notification with provided data.
52*
53* Application authentication key and ID must be set.
54*
55* @param array $data
56*
57* @return array
58*/
59public function add(array $data)
60{
61return $this->api->request('POST', '/notifications', [
62'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
63], json_encode($this->resolve($data)));
64}
65/**
66* Open notification.
67*
68* Application authentication key and ID must be set.
69*
70* @param string $id Notification ID
71*
72* @return array
73*/
74public function open($id)
75{
76return $this->api->request('PUT', '/notifications/'.$id, [
77'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
78], json_encode([
79'app_id' => $this->api->getConfig()->getApplicationId(),
80'opened' => true,
81]));
82}
83/**
84* Cancel notification.
85*
86* Application authentication key and ID must be set.
87*
88* @param string $id Notification ID
89*
90* @return array
91*/
92public function cancel($id)
93{
94$url = '/notifications/'.$id.'?app_id='.$this->api->getConfig()->getApplicationId();
95return $this->api->request('DELETE', $url, [
96'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
97]);
98}
99protected function resolve(array $data)
100{
101$resolver = new OptionsResolver();
102$resolver
103->setDefined('contents')
104->setAllowedTypes('contents', 'array')
105->setDefined('headings')
106->setAllowedTypes('headings', 'array')
107->setDefined('subtitle')
108->setAllowedTypes('subtitle', 'array')
109->setDefined('isIos')
110->setAllowedTypes('isIos', 'bool')
111->setDefined('isAndroid')
112->setAllowedTypes('isAndroid', 'bool')
113->setDefined('isWP')
114->setAllowedTypes('isWP', 'bool')
115->setDefined('isWP_WNS')
116->setAllowedTypes('isWP_WNS', 'bool')
117->setDefined('isAdm')
118->setAllowedTypes('isAdm', 'bool')
119->setDefined('isChrome')
120->setAllowedTypes('isChrome', 'bool')
121->setDefined('isChromeWeb')
122->setAllowedTypes('isChromeWeb', 'bool')
123->setDefined('isFirefox')
124->setAllowedTypes('isFirefox', 'bool')
125->setDefined('isSafari')
126->setAllowedTypes('isSafari', 'bool')
127->setDefined('isAnyWeb')
128->setAllowedTypes('isAnyWeb', 'bool')
129->setDefined('included_segments')
130->setAllowedTypes('included_segments', 'array')
131->setDefined('excluded_segments')
132->setAllowedTypes('excluded_segments', 'array')
133->setDefined('include_player_ids')
134->setAllowedTypes('include_player_ids', 'array')
135->setDefined('include_ios_tokens')
136->setAllowedTypes('include_ios_tokens', 'array')
137->setDefined('include_android_reg_ids')
138->setAllowedTypes('include_android_reg_ids', 'array')
139->setDefined('include_wp_uris')
140->setAllowedTypes('include_wp_uris', 'array')
141->setDefined('include_wp_wns_uris')
142->setAllowedTypes('include_wp_wns_uris', 'array')
143->setDefined('include_amazon_reg_ids')
144->setAllowedTypes('include_amazon_reg_ids', 'array')
145->setDefined('include_chrome_reg_ids')
146->setAllowedTypes('include_chrome_reg_ids', 'array')
147->setDefined('include_chrome_web_reg_ids')
148->setAllowedTypes('include_chrome_web_reg_ids', 'array')
149->setDefined('app_ids')
150->setAllowedTypes('app_ids', 'array')
151->setDefined('filters')
152->setAllowedTypes('filters', 'array')
153->setNormalizer('filters', function (Options $options, array $value) {
154$filters = [];
155foreach ($value as $filter) {
156if (isset($filter['field'])) {
157$filters[] = $filter;
158} elseif (isset($filter['operator'])) {
159$filters[] = ['operator' => 'OR'];
160}
161}
162return $filters;
163})
164->setDefined('ios_badgeType')
165->setAllowedTypes('ios_badgeType', 'string')
166->setAllowedValues('ios_badgeType', ['None', 'SetTo', 'Increase'])
167->setDefined('ios_badgeCount')
168->setAllowedTypes('ios_badgeCount', 'int')
169->setDefined('ios_sound')
170->setAllowedTypes('ios_sound', 'string')
171->setDefined('android_sound')
172->setAllowedTypes('android_sound', 'string')
173->setDefined('adm_sound')
174->setAllowedTypes('adm_sound', 'string')
175->setDefined('wp_sound')
176->setAllowedTypes('wp_sound', 'string')
177->setDefined('wp_wns_sound')
178->setAllowedTypes('wp_wns_sound', 'string')
179->setDefined('data')
180->setAllowedTypes('data', 'array')
181->setDefined('buttons')
182->setAllowedTypes('buttons', 'array')
183->setNormalizer('buttons', function (Options $options, array $value) {
184$buttons = [];
185foreach ($value as $button) {
186if (!isset($button['text'])) {
187continue;
188}
189$buttons[] = [
190'id' => (isset($button['id']) ? $button['id'] : mt_rand()),
191'text' => $button['text'],
192'icon' => (isset($button['icon']) ? $button['icon'] : null),
193];
194}
195return $buttons;
196})
197->setDefined('android_background_layout')
198->setAllowedValues('android_background_layout', 'array')
199->setAllowedValues('android_background_layout', function ($layout) {
200if (empty($layout)) {
201return false;
202}
203$requiredKeys = ['image', 'headings_color', 'contents_color'];
204foreach ($layout as $k => $v) {
205if (!in_array($k, $requiredKeys) || !is_string($v)) {
206return false;
207}
208}
209return true;
210})
211->setDefined('small_icon')
212->setAllowedTypes('small_icon', 'string')
213->setDefined('large_icon')
214->setAllowedTypes('large_icon', 'string')
215->setDefined('ios_attachments')
216->setAllowedTypes('ios_attachments', 'array')
217->setAllowedValues('ios_attachments', function ($attachments) {
218foreach ($attachments as $key => $value) {
219if (!is_string($key) || !is_string($value)) {
220return false;
221}
222}
223return true;
224})
225->setDefined('big_picture')
226->setAllowedTypes('big_picture', 'string')
227->setDefined('adm_small_icon')
228->setAllowedTypes('adm_small_icon', 'string')
229->setDefined('adm_large_icon')
230->setAllowedTypes('adm_large_icon', 'string')
231->setDefined('adm_big_picture')
232->setAllowedTypes('adm_big_picture', 'string')
233->setDefined('web_buttons')
234->setAllowedTypes('web_buttons', 'array')
235->setAllowedValues('web_buttons', function ($buttons) {
236$requiredKeys = ['id', 'text', 'icon', 'url'];
237foreach ($buttons as $button) {
238if (!is_array($button)) {
239return false;
240}
241if (count(array_intersect_key(array_flip($requiredKeys), $button)) != count($requiredKeys)) {
242return false;
243}
244}
245return true;
246})
247->setDefined('ios_category')
248->setAllowedTypes('ios_category', 'string')
249->setDefined('chrome_icon')
250->setAllowedTypes('chrome_icon', 'string')
251->setDefined('chrome_big_picture')
252->setAllowedTypes('chrome_big_picture', 'string')
253->setDefined('chrome_web_icon')
254->setAllowedTypes('chrome_web_icon', 'string')
255->setDefined('chrome_web_image')
256->setAllowedTypes('chrome_web_image', 'string')
257->setDefined('firefox_icon')
258->setAllowedTypes('firefox_icon', 'string')
259->setDefined('url')
260->setAllowedTypes('url', 'string')
261->setAllowedValues('url', function ($value) {
262return (bool) filter_var($value, FILTER_VALIDATE_URL);
263})
264->setDefined('send_after')
265->setAllowedTypes('send_after', '\DateTimeInterface')
266->setNormalizer('send_after', function (Options $options, \DateTime $value) {
267//"2015-09-24 14:00:00 GMT-0700"
268return $value->format('Y-m-d H:i:s TO');
269})
270->setDefined('delayed_option')
271->setAllowedTypes('delayed_option', 'string')
272->setAllowedValues('delayed_option', ['timezone', 'last-active'])
273->setDefined('delivery_time_of_day')
274->setAllowedTypes('delivery_time_of_day', '\DateTimeInterface')
275->setNormalizer('delivery_time_of_day', function (Options $options, \DateTime $value) {
276return $value->format('g:iA');
277})
278->setDefined('android_led_color')
279->setAllowedTypes('android_led_color', 'string')
280->setDefined('android_channel_id')
281->setAllowedTypes('android_channel_id', 'string')
282->setDefined('android_accent_color')
283->setAllowedTypes('android_accent_color', 'string')
284->setDefined('android_visibility')
285->setAllowedTypes('android_visibility', 'int')
286->setAllowedValues('android_visibility', [-1, 0, 1])
287->setDefined('collapse_id')
288->setAllowedTypes('collapse_id', 'string')
289->setDefined('content_available')
290->setAllowedTypes('content_available', 'bool')
291->setDefined('mutable_content')
292->setAllowedTypes('mutable_content', 'bool')
293->setDefined('android_background_data')
294->setAllowedTypes('android_background_data', 'bool')
295->setDefined('amazon_background_data')
296->setAllowedTypes('amazon_background_data', 'bool')
297->setDefined('template_id')
298->setAllowedTypes('template_id', 'string')
299->setDefined('android_group')
300->setAllowedTypes('android_group', 'string')
301->setDefined('android_group_message')
302->setAllowedTypes('android_group_message', 'array')
303->setDefined('adm_group')
304->setAllowedTypes('adm_group', 'string')
305->setDefined('adm_group_message')
306->setAllowedTypes('adm_group_message', 'array')
307->setDefined('ttl')
308->setAllowedTypes('ttl', 'int')
309->setDefined('priority')
310->setAllowedTypes('priority', 'int')
311->setDefault('app_id', $this->api->getConfig()->getApplicationId());
312return $resolver->resolve($data);
313}
314}