1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Pushwoosh; |
4
|
|
|
|
5
|
|
|
use DateTimeInterface; |
6
|
|
|
use DateTimeZone; |
7
|
|
|
use Illuminate\Notifications\Notification; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use JsonSerializable; |
10
|
|
|
|
11
|
|
|
class PushwooshMessage implements JsonSerializable |
12
|
|
|
{ |
13
|
|
|
protected $androidRootParameters; |
14
|
|
|
protected $campaign; |
15
|
|
|
protected $content; |
16
|
|
|
protected $data; |
17
|
|
|
protected $identifier; |
18
|
|
|
protected $iosRootParameters; |
19
|
|
|
protected $preset; |
20
|
|
|
protected $recipientTimezone; |
21
|
|
|
protected $shortenUrl; |
22
|
|
|
protected $timezone; |
23
|
|
|
protected $title; |
24
|
|
|
protected $throughput; |
25
|
|
|
protected $url; |
26
|
|
|
protected $when; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create a new push message. |
30
|
|
|
* |
31
|
|
|
* @param string $content |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
90 |
|
public function __construct(string $content = '') |
35
|
|
|
{ |
36
|
90 |
|
$this->content = $content; |
37
|
90 |
|
$this->recipientTimezone = false; |
38
|
90 |
|
$this->when = 'now'; |
39
|
90 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Associate the message to the given notification. |
43
|
|
|
* |
44
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
12 |
|
public function associate(Notification $notification) |
48
|
|
|
{ |
49
|
12 |
|
if (!$this->identifier) { |
50
|
12 |
|
$this->identifier = $notification->id; |
51
|
|
|
} |
52
|
|
|
|
53
|
12 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the Pushwoosh campaign code. |
58
|
|
|
* |
59
|
|
|
* @param string $campaign |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
6 |
|
public function campaign(string $campaign) |
63
|
|
|
{ |
64
|
6 |
|
$this->campaign = $campaign; |
65
|
|
|
|
66
|
6 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the message content. |
71
|
|
|
* |
72
|
|
|
* @param string $content |
73
|
|
|
* @param string|null $language |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
6 |
|
public function content(string $content, string $language = null) |
77
|
|
|
{ |
78
|
6 |
|
if ($language) { |
79
|
6 |
|
if (!is_array($this->content)) { |
|
|
|
|
80
|
6 |
|
$this->content = []; |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
$this->content[$language] = $content; |
84
|
|
|
} else { |
85
|
6 |
|
$this->content = $content; |
86
|
|
|
} |
87
|
|
|
|
88
|
6 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set the delivery moment. |
93
|
|
|
* |
94
|
|
|
* @param \DateTimeInterface|string $when |
95
|
|
|
* @param \DateTimeZone|string|null $timezone |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
6 |
|
public function deliverAt($when, $timezone = null) |
99
|
|
|
{ |
100
|
6 |
|
if ($when instanceof DateTimeInterface) { |
101
|
6 |
|
$timezone = $when->getTimezone(); |
102
|
6 |
|
$when = $when->format('Y-m-d H:i'); |
103
|
|
|
} |
104
|
|
|
|
105
|
6 |
|
if ($timezone instanceof DateTimeZone) { |
106
|
6 |
|
$timezone = $timezone->getName(); |
107
|
|
|
} |
108
|
|
|
|
109
|
6 |
|
$this->timezone = $timezone; |
110
|
6 |
|
$this->when = $when; |
111
|
|
|
|
112
|
6 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the message identifier. |
117
|
|
|
* |
118
|
|
|
* @param string $identifier |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
12 |
|
public function identifier(string $identifier) |
122
|
|
|
{ |
123
|
12 |
|
$this->identifier = $identifier; |
124
|
|
|
|
125
|
12 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Convert the message into something JSON serializable. |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
78 |
|
public function jsonSerialize() |
134
|
|
|
{ |
135
|
|
|
$payload = [ |
136
|
78 |
|
'android_root_params' => $this->androidRootParameters, |
137
|
78 |
|
'campaign' => $this->campaign, |
138
|
78 |
|
'chrome_title' => $this->title, |
139
|
78 |
|
'content' => $this->content, |
140
|
78 |
|
'data' => $this->data, |
141
|
78 |
|
'firefox_title' => $this->title, |
142
|
78 |
|
'ignore_user_timezone' => !$this->recipientTimezone, |
143
|
78 |
|
'ios_root_params' => $this->iosRootParameters, |
144
|
78 |
|
'ios_title' => $this->title, |
145
|
78 |
|
'link' => $this->url, |
146
|
78 |
|
'minimize_link' => $this->url ? $this->shortenUrl : null, |
147
|
78 |
|
'preset' => $this->preset, |
148
|
78 |
|
'safari_title' => $this->title, |
149
|
78 |
|
'send_date' => $this->when, |
150
|
78 |
|
'send_rate' => $this->throughput, |
151
|
78 |
|
'transactionId' => $this->identifier, |
152
|
78 |
|
'timezone' => $this->timezone, |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
return array_filter($payload, function ($value) { |
156
|
78 |
|
return $value !== null; |
157
|
78 |
|
}); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Set the Pushwoosh preset code. |
162
|
|
|
* |
163
|
|
|
* @param string $preset |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
6 |
|
public function preset(string $preset) |
167
|
|
|
{ |
168
|
6 |
|
$this->preset = $preset; |
169
|
|
|
|
170
|
6 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Set the message title (only supported on Chrome, Firefox, iOS and Safari). |
175
|
|
|
* |
176
|
|
|
* @param string|null $title |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function title(?string $title): self |
180
|
|
|
{ |
181
|
|
|
$this->title = $title; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Throttle the message rollout. |
188
|
|
|
* |
189
|
|
|
* @param int $limit |
190
|
|
|
* @return $this |
191
|
|
|
*/ |
192
|
6 |
|
public function throttle(int $limit) |
193
|
|
|
{ |
194
|
6 |
|
$this->throughput = max(100, min($limit, 1000)); |
195
|
|
|
|
196
|
6 |
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set the URL the message should link to. |
201
|
|
|
* |
202
|
|
|
* @param string $url |
203
|
|
|
* @param bool $shorten |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
6 |
|
public function url(string $url, bool $shorten = true) |
207
|
|
|
{ |
208
|
6 |
|
$this->shortenUrl = $shorten; |
209
|
6 |
|
$this->url = $url; |
210
|
|
|
|
211
|
6 |
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Add a root level parameter. |
216
|
|
|
* |
217
|
|
|
* @param string $key |
218
|
|
|
* @param mixed $value |
219
|
|
|
* @param string|null $platform |
220
|
|
|
* @return $this |
221
|
|
|
*/ |
222
|
18 |
|
public function with(string $key, $value, string $platform = null) |
223
|
|
|
{ |
224
|
18 |
|
if (!in_array($platform, [null, 'ios', 'android'])) { |
225
|
|
|
throw new InvalidArgumentException("Invalid platform {$platform}"); |
226
|
|
|
} |
227
|
|
|
|
228
|
18 |
|
if (($platform ?: 'android') === 'android') { |
229
|
12 |
|
$this->androidRootParameters[$key] = $value; |
230
|
12 |
|
$this->data[$key] = $value; # android_root_params seems to (not always) work |
231
|
|
|
} |
232
|
|
|
|
233
|
18 |
|
if (($platform ?: 'ios') === 'ios') { |
234
|
12 |
|
$this->iosRootParameters[$key] = $value; |
235
|
|
|
} |
236
|
|
|
|
237
|
18 |
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Respect the recipients' timezone when delivering. |
242
|
|
|
* |
243
|
|
|
* @return $this |
244
|
|
|
*/ |
245
|
6 |
|
public function useRecipientTimezone() |
246
|
|
|
{ |
247
|
6 |
|
$this->recipientTimezone = true; |
248
|
|
|
|
249
|
6 |
|
return $this; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|