|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\ExpoPushNotifications; |
|
4
|
|
|
|
|
5
|
|
|
use NotificationChannels\ExpoPushNotifications\Exceptions\CouldNotCreateMessage; |
|
6
|
|
|
|
|
7
|
|
|
class ExpoMessage |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The message title. |
|
11
|
|
|
* |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $title; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The message body. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $body; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The sound to play when the recipient receives this notification. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string|null |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $sound = 'default'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The number to display next to the push notification (iOS). |
|
32
|
|
|
* Specify zero to clear the badge. |
|
33
|
|
|
* |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $badge = 0; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The number of seconds for which the message may be kept around for redelivery if it has not been delivered yet. |
|
40
|
|
|
* |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $ttl = 0; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* ID of the Notification Channel through which to display this notification on Android devices. |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $channelId = ''; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The json data attached to the message. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $jsonData = '{}'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The priority of notification message for Android devices. |
|
61
|
|
|
* |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $priority = 'default'; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Create a message with given body. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $body |
|
70
|
|
|
* |
|
71
|
|
|
* @return static |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public static function create($body = '') |
|
74
|
|
|
{ |
|
75
|
1 |
|
return new static($body); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* ExpoMessage constructor. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $body |
|
82
|
|
|
*/ |
|
83
|
13 |
|
public function __construct(string $body = '') |
|
84
|
|
|
{ |
|
85
|
13 |
|
$this->body = $body; |
|
86
|
13 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Set the message title. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $value |
|
92
|
|
|
* |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
|
|
public function title(string $value) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->title = $value; |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Set the message body. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $value |
|
106
|
|
|
* |
|
107
|
|
|
* @return $this |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function body(string $value) |
|
110
|
|
|
{ |
|
111
|
1 |
|
$this->body = $value; |
|
112
|
|
|
|
|
113
|
1 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Enable the message sound. |
|
118
|
|
|
* |
|
119
|
|
|
* @return $this |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function enableSound() |
|
122
|
|
|
{ |
|
123
|
1 |
|
$this->sound = 'default'; |
|
124
|
|
|
|
|
125
|
1 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Disable the message sound. |
|
130
|
|
|
* |
|
131
|
|
|
* @return $this |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function disableSound() |
|
134
|
|
|
{ |
|
135
|
2 |
|
$this->sound = null; |
|
136
|
|
|
|
|
137
|
2 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Set the message badge (iOS). |
|
142
|
|
|
* |
|
143
|
|
|
* @param int $value |
|
144
|
|
|
* |
|
145
|
|
|
* @return $this |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function badge(int $value) |
|
148
|
|
|
{ |
|
149
|
1 |
|
$this->badge = $value; |
|
150
|
|
|
|
|
151
|
1 |
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Set the time to live of the notification. |
|
156
|
|
|
* |
|
157
|
|
|
* @param int $ttl |
|
158
|
|
|
* |
|
159
|
|
|
* @return $this |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function setTtl(int $ttl) |
|
162
|
|
|
{ |
|
163
|
1 |
|
$this->ttl = $ttl; |
|
164
|
|
|
|
|
165
|
1 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Set the channelId of the notification for Android devices. |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $channelId |
|
172
|
|
|
* |
|
173
|
|
|
* @return $this |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function setChannelId(string $channelId) |
|
176
|
|
|
{ |
|
177
|
1 |
|
$this->channelId = $channelId; |
|
178
|
|
|
|
|
179
|
1 |
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Set the json Data attached to the message. |
|
184
|
|
|
* |
|
185
|
|
|
* @param array|string $data |
|
186
|
|
|
* |
|
187
|
|
|
* @return $this |
|
188
|
|
|
* |
|
189
|
|
|
* @throws CouldNotCreateMessage |
|
190
|
|
|
*/ |
|
191
|
1 |
|
public function setJsonData($data) |
|
192
|
|
|
{ |
|
193
|
1 |
|
if (is_array($data)) { |
|
194
|
|
|
$data = json_encode($data); |
|
195
|
1 |
|
} elseif (is_string($data)) { |
|
196
|
1 |
|
@json_decode($data); |
|
|
|
|
|
|
197
|
|
|
|
|
198
|
1 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
199
|
|
|
throw new CouldNotCreateMessage('Invalid json format passed to the setJsonData().'); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
$this->jsonData = $data; |
|
204
|
|
|
|
|
205
|
1 |
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Set the priority of the notification, must be one of [default, normal, high]. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $priority |
|
212
|
|
|
* |
|
213
|
|
|
* @return $this |
|
214
|
|
|
*/ |
|
215
|
|
|
public function priority(string $priority) |
|
216
|
|
|
{ |
|
217
|
|
|
$this->priority = $priority; |
|
218
|
|
|
|
|
219
|
|
|
return $this; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Get an array representation of the message. |
|
224
|
|
|
* |
|
225
|
|
|
* @return array |
|
226
|
|
|
*/ |
|
227
|
13 |
|
public function toArray() |
|
228
|
|
|
{ |
|
229
|
|
|
$message = [ |
|
230
|
13 |
|
'title' => $this->title, |
|
231
|
13 |
|
'body' => $this->body, |
|
232
|
13 |
|
'sound' => $this->sound, |
|
233
|
13 |
|
'badge' => $this->badge, |
|
234
|
13 |
|
'ttl' => $this->ttl, |
|
235
|
13 |
|
'data' => $this->jsonData, |
|
236
|
13 |
|
'priority' => $this->priority, |
|
237
|
|
|
]; |
|
238
|
13 |
|
if (! empty($this->channelId)) { |
|
239
|
1 |
|
$message['channelId'] = $this->channelId; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
13 |
|
return $message; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: