1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Gcm; |
4
|
|
|
|
5
|
|
|
class GcmMessage |
6
|
|
|
{ |
7
|
|
|
const PRIORITY_NORMAL = 'normal'; |
8
|
|
|
const PRIORITY_HIGH = 'high'; |
9
|
|
|
const ANDROID = 'android'; |
10
|
|
|
const IOS = 'ios'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The title of the notification. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
public $title; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The message of the notification. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $message; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The badge of the notification. |
28
|
|
|
* @warning UNUSED |
29
|
|
|
* |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
public $badge; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The priority of the notification. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $priority = self::PRIORITY_NORMAL; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Additional data of the notification. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
public $data = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set the 'OS' to send the notification. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $os = self::ANDROID; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string|null $title |
57
|
|
|
* @param string|null $message |
58
|
|
|
* @param array $data |
59
|
|
|
* @param string $priority |
60
|
|
|
* |
61
|
|
|
* @return static |
62
|
|
|
*/ |
63
|
1 |
|
public static function create($title = null, $message = null, $data = [], $priority = self::PRIORITY_NORMAL) |
64
|
|
|
{ |
65
|
1 |
|
return new static($title, $message, $data, $priority); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string|null $title |
70
|
|
|
* @param string|null $message |
71
|
|
|
* @param array $data |
72
|
|
|
* @param string $priority |
73
|
|
|
*/ |
74
|
10 |
|
public function __construct($title = null, $message = null, $data = [], $priority = self::PRIORITY_NORMAL) |
75
|
|
|
{ |
76
|
10 |
|
$this->title = $title; |
77
|
10 |
|
$this->message = $message; |
78
|
10 |
|
$this->data = $data; |
79
|
10 |
|
$this->priority = $priority; |
80
|
10 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set the title of the notification. |
84
|
|
|
* |
85
|
|
|
* @param string $title |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
1 |
|
public function title($title) |
90
|
|
|
{ |
91
|
1 |
|
$this->title = $title; |
92
|
|
|
|
93
|
1 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the message of the notification. |
98
|
|
|
* |
99
|
|
|
* @param string $message |
100
|
|
|
* |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
1 |
|
public function message($message) |
104
|
|
|
{ |
105
|
1 |
|
$this->message = $message; |
106
|
|
|
|
107
|
1 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set the badge of the notification. |
112
|
|
|
* |
113
|
|
|
* @param int $badge |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function badge($badge) |
118
|
|
|
{ |
119
|
|
|
$this->badge = $badge; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set the priority of the notification. |
126
|
|
|
* |
127
|
|
|
* @param string $priority |
128
|
|
|
* |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
1 |
|
public function priority($priority) |
132
|
|
|
{ |
133
|
1 |
|
$this->priority = $priority; |
134
|
|
|
|
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Add data to the notification. |
140
|
|
|
* |
141
|
|
|
* @param string $key |
142
|
|
|
* @param mixed $value |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
1 |
|
public function data($key, $value) |
147
|
|
|
{ |
148
|
1 |
|
$this->data[$key] = $value; |
149
|
|
|
|
150
|
1 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Override the data of the notification. |
155
|
|
|
* |
156
|
|
|
* @param array $data |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
|
|
public function setData($data) |
160
|
|
|
{ |
161
|
|
|
$this->data = $data; |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Add an action to the notification. |
168
|
|
|
* |
169
|
|
|
* @param string $action |
170
|
|
|
* @param mixed $params |
171
|
|
|
* |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function action($action, $params = null) |
175
|
|
|
{ |
176
|
|
|
return $this->data('action', [ |
177
|
|
|
'action' => $action, |
178
|
|
|
'params' => $params, |
179
|
|
|
]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set the OS to send the notification. |
184
|
|
|
* |
185
|
|
|
* @param string $os |
186
|
|
|
* |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
2 |
|
public function os($os) |
190
|
|
|
{ |
191
|
2 |
|
$this->os = $os; |
192
|
|
|
|
193
|
2 |
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Check if the current os is Android. |
198
|
|
|
* |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
1 |
|
public function isAndroid() |
202
|
|
|
{ |
203
|
1 |
|
return $this->os === self::ANDROID; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Check if the current os is IOS. |
208
|
|
|
* |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
1 |
|
public function isIOS() |
212
|
|
|
{ |
213
|
1 |
|
return $this->os === self::IOS; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|