1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\PusherPushNotifications; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use NotificationChannels\PusherPushNotifications\Exceptions\CouldNotCreateMessage; |
7
|
|
|
|
8
|
|
|
class Message |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The device platform (iOS/Android). |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $platform = 'iOS'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The message title. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $title; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The message body. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $body; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The phone number the message should be sent from. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $sound = 'default'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The message icon (Android). |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $icon; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The number to display next to the push notification (iOS). |
47
|
|
|
* |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
protected $badge; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Options. |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected $options = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $body |
61
|
|
|
* |
62
|
|
|
* @return static |
63
|
|
|
*/ |
64
|
|
|
public static function create($body = '') |
65
|
|
|
{ |
66
|
|
|
return new static($body); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $body |
71
|
|
|
*/ |
72
|
|
|
public function __construct($body = '') |
73
|
|
|
{ |
74
|
|
|
$this->body = $body; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set the platform [iOS/Android]. |
79
|
|
|
* |
80
|
|
|
* @param string $platform |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
* |
84
|
|
|
* @throws \NotificationChannels\PusherPushNotifications\Exceptions\CouldNotCreateMessage |
85
|
|
|
*/ |
86
|
|
|
public function platform($platform) |
87
|
|
|
{ |
88
|
|
|
if (! in_array($platform, ['iOS', 'Android'])) { |
89
|
|
|
throw CouldNotCreateMessage::invalidPlatformGiven($platform); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->platform = $platform; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the platform to iOS. |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function iOS() |
103
|
|
|
{ |
104
|
|
|
$this->platform = 'iOS'; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set the platform to Android. |
111
|
|
|
* |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
public function android() |
115
|
|
|
{ |
116
|
|
|
$this->platform = 'Android'; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set the message title. |
123
|
|
|
* |
124
|
|
|
* @param string $value |
125
|
|
|
* |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function title($value) |
129
|
|
|
{ |
130
|
|
|
$this->title = $value; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set the message body. |
137
|
|
|
* |
138
|
|
|
* @param string $value |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function body($value) |
143
|
|
|
{ |
144
|
|
|
$this->body = $value; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set the message sound (Android). |
151
|
|
|
* |
152
|
|
|
* @param string $value |
153
|
|
|
* |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
public function sound($value) |
157
|
|
|
{ |
158
|
|
|
$this->sound = $value; |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Set the message icon (Android). |
165
|
|
|
* |
166
|
|
|
* @param string $value |
167
|
|
|
* |
168
|
|
|
* @return $this |
169
|
|
|
*/ |
170
|
|
|
public function icon($value) |
171
|
|
|
{ |
172
|
|
|
$this->icon = $value; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Set the message badge (iOS). |
179
|
|
|
* |
180
|
|
|
* @param int $value |
181
|
|
|
* |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
|
|
public function badge($value) |
185
|
|
|
{ |
186
|
|
|
$this->badge = (int) $value; |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $key |
193
|
|
|
* @param mixed $value |
194
|
|
|
* |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
|
|
public function setOption($key, $value) |
198
|
|
|
{ |
199
|
|
|
$this->options[$key] = $value; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get an array representation of the message. |
206
|
|
|
* |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
public function toArray() |
210
|
|
|
{ |
211
|
|
|
return $this->platform === 'iOS' |
212
|
|
|
? $this->toiOS() |
213
|
|
|
: $this->toAndroid(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Format the message for iOS. |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
View Code Duplication |
public function toiOS() |
|
|
|
|
222
|
|
|
{ |
223
|
|
|
$message = [ |
224
|
|
|
'apns' => [ |
225
|
|
|
'aps' => [ |
226
|
|
|
'alert' => [ |
227
|
|
|
'title' => $this->title, |
228
|
|
|
'body' => $this->body, |
229
|
|
|
], |
230
|
|
|
'sound' => $this->sound, |
231
|
|
|
'badge' => $this->badge, |
232
|
|
|
], |
233
|
|
|
], |
234
|
|
|
]; |
235
|
|
|
|
236
|
|
|
foreach ($this->options as $option => $value) { |
237
|
|
|
Arr::set($message, $option, $value); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $message; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Format the message for Android. |
245
|
|
|
* |
246
|
|
|
* @return array |
247
|
|
|
*/ |
248
|
|
View Code Duplication |
public function toAndroid() |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
$message = [ |
251
|
|
|
'gcm' => [ |
252
|
|
|
'notification' => [ |
253
|
|
|
'title' => $this->title, |
254
|
|
|
'body' => $this->body, |
255
|
|
|
'sound' => $this->sound, |
256
|
|
|
'icon' => $this->icon, |
257
|
|
|
], |
258
|
|
|
], |
259
|
|
|
]; |
260
|
|
|
|
261
|
|
|
foreach ($this->options as $option => $value) { |
262
|
|
|
Arr::set($message, $option, $value); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return $message; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.