1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\ExpoPushNotifications; |
4
|
|
|
|
5
|
|
|
use NotificationChannels\ExpoPushNotifications\Exceptions\CouldNotCreateMessage; |
6
|
|
|
|
7
|
|
|
class ExpoMessage |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The message body. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $body; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The sound to play when the recipient receives this notification. |
18
|
|
|
* |
19
|
|
|
* @var string|null |
20
|
|
|
*/ |
21
|
|
|
protected $sound = 'default'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The number to display next to the push notification (iOS). |
25
|
|
|
* Specify zero to clear the badge. |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $badge = 0; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The number of seconds for which the message may be kept around for redelivery if it has not been delivered yet. |
33
|
|
|
* |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $ttl = 0; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The json data attached to the message. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $jsonData = ''; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create a message with given body. |
47
|
|
|
* |
48
|
|
|
* @param string $body |
49
|
|
|
* |
50
|
|
|
* @return static |
51
|
|
|
*/ |
52
|
1 |
|
public static function create($body = '') |
53
|
|
|
{ |
54
|
1 |
|
return new static($body); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* ExpoMessage constructor. |
59
|
|
|
* |
60
|
|
|
* @param string $body |
61
|
|
|
*/ |
62
|
10 |
|
public function __construct(string $body = '') |
63
|
|
|
{ |
64
|
10 |
|
$this->body = $body; |
65
|
10 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set the message body. |
69
|
|
|
* |
70
|
|
|
* @param string $value |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
1 |
|
public function body(string $value) |
75
|
|
|
{ |
76
|
1 |
|
$this->body = $value; |
77
|
|
|
|
78
|
1 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Enable the message sound. |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
1 |
|
public function enableSound() |
87
|
|
|
{ |
88
|
1 |
|
$this->sound = 'default'; |
89
|
|
|
|
90
|
1 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Disable the message sound. |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
2 |
|
public function disableSound() |
99
|
|
|
{ |
100
|
2 |
|
$this->sound = null; |
101
|
|
|
|
102
|
2 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the message badge (iOS). |
107
|
|
|
* |
108
|
|
|
* @param int $value |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
1 |
|
public function badge(int $value) |
113
|
|
|
{ |
114
|
1 |
|
$this->badge = $value; |
115
|
|
|
|
116
|
1 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set the time to live of the notification. |
121
|
|
|
* |
122
|
|
|
* @param int $ttl |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
1 |
|
public function setTtl(int $ttl) |
127
|
|
|
{ |
128
|
1 |
|
$this->ttl = $ttl; |
129
|
|
|
|
130
|
1 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the json Data attached to the message. |
135
|
|
|
* |
136
|
|
|
* @param array|string $data |
137
|
|
|
* |
138
|
|
|
* @return $this |
139
|
|
|
* |
140
|
|
|
* @throws CouldNotCreateMessage |
141
|
|
|
*/ |
142
|
1 |
|
public function setJsonData($data) |
143
|
|
|
{ |
144
|
1 |
|
if (is_array($data)) { |
145
|
|
|
$data = json_encode($data); |
146
|
1 |
|
} elseif (is_string($data)) { |
147
|
1 |
|
@json_decode($data); |
|
|
|
|
148
|
|
|
|
149
|
1 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
150
|
|
|
throw new CouldNotCreateMessage('Invalid json format passed to the setJsonData().'); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
$this->jsonData = $data; |
155
|
|
|
|
156
|
1 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get an array representation of the message. |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
10 |
|
public function toArray() |
165
|
|
|
{ |
166
|
|
|
return [ |
167
|
10 |
|
'body' => $this->body, |
168
|
10 |
|
'sound' => $this->sound, |
169
|
10 |
|
'badge' => $this->badge, |
170
|
10 |
|
'ttl' => $this->ttl, |
171
|
10 |
|
'jsonData' => $this->jsonData, |
172
|
|
|
]; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: