1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\OneSignal; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
class OneSignalMessage |
8
|
|
|
{ |
9
|
|
|
/** @var string */ |
10
|
|
|
protected $body; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
protected $subject; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
protected $url; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $icon; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
protected $data = []; |
23
|
|
|
|
24
|
|
|
/** @var array */ |
25
|
|
|
protected $buttons = []; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
protected $webButtons = []; |
29
|
|
|
|
30
|
|
|
/** @var array */ |
31
|
|
|
protected $extraParameters = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $body |
35
|
|
|
* |
36
|
|
|
* @return static |
37
|
|
|
*/ |
38
|
1 |
|
public static function create($body = '') |
39
|
|
|
{ |
40
|
1 |
|
return new static($body); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $body |
45
|
|
|
*/ |
46
|
20 |
|
public function __construct($body = '') |
47
|
|
|
{ |
48
|
20 |
|
$this->body = $body; |
49
|
20 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set the message body. |
53
|
|
|
* |
54
|
|
|
* @param string $value |
55
|
|
|
* |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
1 |
|
public function body($value) |
59
|
|
|
{ |
60
|
1 |
|
$this->body = $value; |
61
|
|
|
|
62
|
1 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the message icon. |
67
|
|
|
* |
68
|
|
|
* @param string $value |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
6 |
|
public function icon($value) |
73
|
|
|
{ |
74
|
6 |
|
$this->icon = $value; |
75
|
|
|
|
76
|
6 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the message subject. |
81
|
|
|
* |
82
|
|
|
* @param string $value |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
6 |
|
public function subject($value) |
87
|
|
|
{ |
88
|
6 |
|
$this->subject = $value; |
89
|
|
|
|
90
|
6 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the message url. |
95
|
|
|
* |
96
|
|
|
* @param string $value |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
6 |
|
public function url($value) |
101
|
|
|
{ |
102
|
6 |
|
$this->url = $value; |
103
|
|
|
|
104
|
6 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the iOS badge increment count. |
109
|
|
|
* |
110
|
|
|
* @param int $count |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
1 |
|
public function incrementIosBadgeCount($count = 1) |
115
|
|
|
{ |
116
|
1 |
|
return $this->setParameter('ios_badgeType', 'Increase') |
117
|
1 |
|
->setParameter('ios_badgeCount', $count); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the iOS badge decrement count. |
122
|
|
|
* |
123
|
|
|
* @param int $count |
124
|
|
|
* |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
1 |
|
public function decrementIosBadgeCount($count = 1) |
128
|
|
|
{ |
129
|
1 |
|
return $this->setParameter('ios_badgeType', 'Increase') |
130
|
1 |
|
->setParameter('ios_badgeCount', -1 * $count); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the iOS badge count. |
135
|
|
|
* |
136
|
|
|
* @param int $count |
137
|
|
|
* |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
1 |
|
public function setIosBadgeCount($count) |
141
|
|
|
{ |
142
|
1 |
|
return $this->setParameter('ios_badgeType', 'SetTo') |
143
|
1 |
|
->setParameter('ios_badgeCount', $count); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set additional data. |
148
|
|
|
* |
149
|
|
|
* @param string $key |
150
|
|
|
* @param string $value |
151
|
|
|
* |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
1 |
|
public function setData($key, $value) |
155
|
|
|
{ |
156
|
1 |
|
$this->data[$key] = $value; |
157
|
|
|
|
158
|
1 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set additional parameters. |
163
|
|
|
* |
164
|
|
|
* @param string $key |
165
|
|
|
* @param string $value |
166
|
|
|
* |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
4 |
|
public function setParameter($key, $value) |
170
|
|
|
{ |
171
|
4 |
|
$this->extraParameters[$key] = $value; |
172
|
|
|
|
173
|
4 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Add a web button to the message. |
178
|
|
|
* |
179
|
|
|
* @param OneSignalWebButton $button |
180
|
|
|
* |
181
|
|
|
* @return $this |
182
|
|
|
*/ |
183
|
1 |
|
public function webButton(OneSignalWebButton $button) |
184
|
|
|
{ |
185
|
1 |
|
$this->webButtons[] = $button->toArray(); |
186
|
|
|
|
187
|
1 |
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Add a native button to the message. |
192
|
|
|
* |
193
|
|
|
* @param OneSignalButton $button |
194
|
|
|
* |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
1 |
|
public function button(OneSignalButton $button) |
198
|
|
|
{ |
199
|
1 |
|
$this->buttons[] = $button->toArray(); |
200
|
|
|
|
201
|
1 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Set an image to all possible attachment variables. |
206
|
|
|
* @param string $imageUrl |
207
|
|
|
* |
208
|
|
|
* @return $this |
209
|
|
|
*/ |
210
|
1 |
|
public function setImageAttachments($imageUrl) |
211
|
|
|
{ |
212
|
1 |
|
$this->extraParameters['ios_attachments']['id1'] = $imageUrl; |
213
|
1 |
|
$this->extraParameters['big_picture'] = $imageUrl; |
214
|
1 |
|
$this->extraParameters['adm_big_picture'] = $imageUrl; |
215
|
1 |
|
$this->extraParameters['chrome_big_picture'] = $imageUrl; |
216
|
|
|
|
217
|
1 |
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
20 |
|
public function toArray() |
224
|
|
|
{ |
225
|
|
|
$message = [ |
226
|
20 |
|
'contents' => ['en' => $this->body], |
227
|
20 |
|
'headings' => $this->subjectToArray(), |
228
|
20 |
|
'url' => $this->url, |
229
|
20 |
|
'buttons' => $this->buttons, |
230
|
20 |
|
'web_buttons' => $this->webButtons, |
231
|
20 |
|
'chrome_web_icon' => $this->icon, |
232
|
20 |
|
'chrome_icon' => $this->icon, |
233
|
20 |
|
'adm_small_icon' => $this->icon, |
234
|
20 |
|
'small_icon' => $this->icon, |
235
|
|
|
]; |
236
|
|
|
|
237
|
20 |
|
foreach ($this->extraParameters as $key => $value) { |
238
|
5 |
|
Arr::set($message, $key, $value); |
239
|
|
|
} |
240
|
|
|
|
241
|
20 |
|
foreach ($this->data as $data => $value) { |
242
|
1 |
|
Arr::set($message, 'data.'.$data, $value); |
243
|
|
|
} |
244
|
|
|
|
245
|
20 |
|
return $message; |
246
|
|
|
} |
247
|
|
|
|
248
|
20 |
|
protected function subjectToArray() |
249
|
|
|
{ |
250
|
20 |
|
if ($this->subject === null) { |
251
|
14 |
|
return []; |
252
|
|
|
} |
253
|
|
|
|
254
|
6 |
|
return ['en' => $this->subject]; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|