This repository was archived by the owner on Oct 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathMessage.php
executable file
·309 lines (266 loc) · 7.99 KB
/
Message.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
namespace PHP_GCM;
class Message {
const COLLAPSE_KEY = 'collapse_key';
const TIME_TO_LIVE = 'time_to_live';
const DRY_RUN = 'dry_run';
const DELAY_WHILE_IDLE = 'delay_while_idle';
const RESTRICTED_PACKAGE_NAME = 'restricted_package_name';
const REGISTRATION_IDS = 'registration_ids';
const DATA = 'data';
const TO = 'to';
const NOTIFICATION = 'notification';
const NOTIFICATION_TITLE = 'title';
const NOTIFICATION_BODY = 'body';
const NOTIFICATION_ICON = 'icon';
const NOTIFICATION_SOUND = 'sound';
const NOTIFICATION_BADGE = 'badge';
const NOTIFICATION_TAG = 'tag';
const NOTIFICATION_COLOR = 'color';
const NOTIFICATION_CLICK_ACTION = 'click_action';
const NOTIFICATION_BODY_LOC_KEY = 'body_loc_key';
const NOTIFICATION_BODY_LOC_ARGS = 'body_loc_args';
const NOTIFICATION_TITLE_LOC_KEY = 'title_loc_key';
const NOTIFICATION_TITLE_LOC_ARGS = 'title_loc_args';
const CONTENT_AVAILABLE = 'content_available';
const PRIORITY = 'priority';
const RAW_DATA = 'raw_data';
private $collapseKey;
private $delayWhileIdle;
private $dryRun;
private $timeToLive;
private $data;
private $restrictedPackageName;
private $notification;
private $contentAvailable;
private $priority;
private $rawData;
private $publicKey;
private $salt;
/**
* Message Constructor
*/
public function __construct() {
$this->timeToLive = 2419200;
$this->delayWhileIdle = false;
$this->dryRun = false;
}
/**
* Sets the collapseKey property.
*
* @param string $collapseKey
* @return Message Returns the instance of this Message for method chaining.
*/
public function collapseKey($collapseKey) {
$this->collapseKey = $collapseKey;
return $this;
}
public function getCollapseKey() {
return $this->collapseKey;
}
/**
* Sets the delayWhileIdle property (default value is {false}).
*
* @param bool $delayWhileIdle
* @return Message Returns the instance of this Message for method chaining.
*/
public function delayWhileIdle($delayWhileIdle) {
$this->delayWhileIdle = $delayWhileIdle;
return $this;
}
public function getDelayWhileIdle() {
if(isset($this->delayWhileIdle))
return $this->delayWhileIdle;
return null;
}
/**
* Sets the dryRun property (default value is {false}).
*
* @param bool $dryRun
* @return Message Returns the instance of this Message for method chaining.
*/
public function dryRun($dryRun) {
$this->dryRun = $dryRun;
return $this;
}
public function getDryRun() {
return $this->dryRun;
}
/**
* Sets the time to live, in seconds.
*
* @param int $timeToLive
* @return Message Returns the instance of this Message for method chaining.
*/
public function timeToLive($timeToLive) {
$this->timeToLive = $timeToLive;
return $this;
}
public function getTimeToLive() {
return $this->timeToLive;
}
/**
* Adds a key/value pair to the payload data.
*
* @param string $key
* @param string $value
* @return Message Returns the instance of this Message for method chaining.
*/
public function addData($key, $value) {
$this->data[$key] = $value;
return $this;
}
/**
* Sets the data property
*
* @param array $data
* @return Message Returns the instance of this Message for method chaining.
*/
public function data(array $data) {
$this->data = $data;
return $this;
}
public function getData() {
return $this->data;
}
/**
* Sets the restrictedPackageName property.
*
* @param string $restrictedPackageName
* @return Message Returns the instance of this Message for method chaining.
*/
public function restrictedPackageName($restrictedPackageName) {
$this->restrictedPackageName = $restrictedPackageName;
return $this;
}
public function getRestrictedPackageName() {
return $this->restrictedPackageName;
}
/**
* Sets the notification for the message. See the Notification class for more information.
*
* @param Notification $notification
* @return Message Returns the instance of this Message for method chaining.
*/
public function notification(Notification $notification) {
$this->notification = $notification;
return $this;
}
public function getNotification() {
return $this->notification;
}
/**
* Sets the contentAvailable property
*
* @param $contentAvailable
* @return $this
*/
public function contentAvailable($contentAvailable) {
$this->contentAvailable = $contentAvailable;
return $this;
}
public function getContentAvailable() {
return $this->contentAvailable;
}
/**
* Sets the priority property
*
* @param $priority
* @return $this
*/
public function priority($priority) {
$this->priority = $priority;
return $this;
}
public function getPriority() {
return $this->priority;
}
/**
* Sets the rawData property
*
* @param $rawData
* @return $this
*/
public function rawData($rawData) {
$this->rawData = $rawData;
return $this;
}
public function getRawData() {
return $this->rawData;
}
/**
* Sets the publicKey property
*
* @param $publicKey
* @return $this
*/
public function publicKey($publicKey) {
$this->publicKey = $publicKey;
return $this;
}
public function getPublicKey() {
return $this->publicKey;
}
/**
* Sets the salt property
*
* @param $salt
* @return $this
*/
public function salt($salt) {
$this->salt = $salt;
return $this;
}
public function getSalt() {
return $this->salt;
}
public function build($recipients) {
$message = array();
if (!is_array($recipients)) {
$message[self::TO] = $recipients;
} else if (count($recipients) == 1) {
$message[self::TO] = $recipients[0];
} else {
$message[self::REGISTRATION_IDS] = $recipients;
}
if (!empty($this->collapseKey)) {
$message[self::COLLAPSE_KEY] = $this->collapseKey;
}
$message[self::DELAY_WHILE_IDLE] = $this->delayWhileIdle;
$message[self::TIME_TO_LIVE] = $this->timeToLive;
$message[self::DRY_RUN] = $this->dryRun;
if (!empty($this->restrictedPackageName)) {
$message[self::RESTRICTED_PACKAGE_NAME] = $this->restrictedPackageName;
}
if (!is_null($this->contentAvailable)) {
$message[self::CONTENT_AVAILABLE] = $this->contentAvailable;
}
if ($this->priority) {
$message[self::PRIORITY] = $this->priority;
}
if (!is_null($this->data) && count($this->data) > 0) {
$message[self::DATA] = $this->data;
}
if ($this->notification != null) {
$message[self::NOTIFICATION] = array();
if ($this->notification->getBadge() != null) {
$message[self::NOTIFICATION][self::NOTIFICATION_BADGE] = $this->notification->getBadge();
}
$message[self::NOTIFICATION][self::NOTIFICATION_BODY] = $this->notification->getBody();
$message[self::NOTIFICATION][self::NOTIFICATION_BODY_LOC_ARGS] = $this->notification->getBodyLocArgs();
$message[self::NOTIFICATION][self::NOTIFICATION_BODY_LOC_KEY] = $this->notification->getBodyLocKey();
$message[self::NOTIFICATION][self::NOTIFICATION_CLICK_ACTION] = $this->notification->getClickAction();
$message[self::NOTIFICATION][self::NOTIFICATION_COLOR] = $this->notification->getColor();
$message[self::NOTIFICATION][self::NOTIFICATION_ICON] = $this->notification->getIcon();
$message[self::NOTIFICATION][self::NOTIFICATION_SOUND] = $this->notification->getSound();
$message[self::NOTIFICATION][self::NOTIFICATION_TAG] = $this->notification->getTag();
$message[self::NOTIFICATION][self::NOTIFICATION_TITLE] = $this->notification->getTitle();
$message[self::NOTIFICATION][self::NOTIFICATION_TITLE_LOC_ARGS] = $this->notification->getTitleLocArgs();
$message[self::NOTIFICATION][self::NOTIFICATION_TITLE_LOC_KEY] = $this->notification->getTitleLocKey();
}
if (!empty($this->rawData)) {
$message[self::RAW_DATA] = $this->rawData;
}
return json_encode($message);
}
}