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
|
|
|
/** |
31
|
|
|
* @param string $body |
32
|
|
|
* |
33
|
|
|
* @return static |
34
|
|
|
*/ |
35
|
|
|
public static function create($body = '') |
36
|
|
|
{ |
37
|
|
|
return new static($body); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $body |
42
|
|
|
*/ |
43
|
|
|
public function __construct($body = '') |
44
|
|
|
{ |
45
|
|
|
$this->body = $body; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set the message body. |
50
|
|
|
* |
51
|
|
|
* @param string $value |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function body($value) |
56
|
|
|
{ |
57
|
|
|
$this->body = $value; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the message icon. |
64
|
|
|
* |
65
|
|
|
* @param string $value |
66
|
|
|
* |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function icon($value) |
70
|
|
|
{ |
71
|
|
|
$this->icon = $value; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the message subject. |
78
|
|
|
* |
79
|
|
|
* @param string $value |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function subject($value) |
84
|
|
|
{ |
85
|
|
|
$this->subject = $value; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set the message url. |
92
|
|
|
* |
93
|
|
|
* @param string $value |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function url($value) |
98
|
|
|
{ |
99
|
|
|
$this->url = $value; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set additional data. |
106
|
|
|
* |
107
|
|
|
* @param string $key |
108
|
|
|
* @param string $value |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function setData($key, $value) |
113
|
|
|
{ |
114
|
|
|
$this->data[$key] = $value; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Add a web button to the message. |
121
|
|
|
* |
122
|
|
|
* @param string $id |
123
|
|
|
* @param string $text |
124
|
|
|
* @param string $icon |
125
|
|
|
* @param string $url |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function webButton($id, $text, $icon, $url) |
130
|
|
|
{ |
131
|
|
|
$this->webButtons[] = [ |
132
|
|
|
'id' => $id, |
133
|
|
|
'text' => $text, |
134
|
|
|
'icon' => $icon, |
135
|
|
|
'url' => $url, |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Add a native button to the message. |
143
|
|
|
* |
144
|
|
|
* @param string $id |
145
|
|
|
* @param string $text |
146
|
|
|
* @param string $icon |
147
|
|
|
* |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function button($id, $text, $icon) |
151
|
|
|
{ |
152
|
|
|
$this->buttons[] = [ |
153
|
|
|
'id' => $id, |
154
|
|
|
'text' => $text, |
155
|
|
|
'icon' => $icon, |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function toArray() |
165
|
|
|
{ |
166
|
|
|
$message = [ |
167
|
|
|
'contents' => ['en' => $this->body], |
168
|
|
|
'headings' => ['en' => $this->subject], |
169
|
|
|
'url' => $this->url, |
170
|
|
|
'buttons' => $this->buttons, |
171
|
|
|
'web_buttons' => $this->webButtons, |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
foreach ($this->data as $data => $value) { |
175
|
|
|
Arr::set($message, 'data.'.$data, $value); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $message; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|