-
Notifications
You must be signed in to change notification settings - Fork 29
/
urbanairship.php
217 lines (188 loc) · 6.63 KB
/
urbanairship.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
<?php
// Php module for using the Urban Airship API
require_once 'RESTClient.php';
define('SERVER', 'go.urbanairship.com');
define('BASE_URL', 'https://go.urbanairship.com/api');
define('DEVICE_TOKEN_URL', BASE_URL . '/device_tokens/');
define('PUSH_URL', BASE_URL . '/push/');
define('BROADCAST_URL', BASE_URL . '/push/broadcast/');
define('FEEDBACK_URL', BASE_URL . '/device_tokens/feedback/');
// Raise when we get a 401 from the server.
class Unauthorized extends Exception {
}
// Raise when we get an error response from the server.
// args are (status code, message).
class AirshipFailure extends Exception {
}
class AirshipDeviceList implements Iterator, Countable {
private $_airship = null;
private $_page = null;
private $_position = 0;
public function __construct($airship) {
$this->_airship = $airship;
$this->_load_page(DEVICE_TOKEN_URL);
$this->_position = 0;
}
private function _load_page($url) {
$response = $this->_airship->_request($url, 'GET', null, null);
$response_code = $response[0];
if ($response_code != 200) {
throw new AirshipFailure($response[1], $response_code);
}
$result = json_decode($response[1]);
var_dump($result->next_page);
if ($this->_page == null) {
$this->_page = $result;
} else {
echo 'got next page of device tokens';
$this->_page->device_tokens = array_merge($this->_page->device_tokens, $result->device_tokens);
$this->_page->next_page = $result->next_page;
}
}
// Countable interface
public function count() {
return $this->_page->device_tokens_count;
}
// Iterator interface
function rewind() {
$this->_position = 0;
}
function current() {
return $this->_page->device_tokens[$this->_position];
}
function key() {
return $this->_position;
}
function next() {
++$this->_position;
}
function valid() {
if (!isset($this->_page->device_tokens[$this->_position])) {
$next_page = $this->_page->next_page;
if ($next_page == null) {
return false;
} else {
$this->_load_page($next_page);
return $this->valid();
}
}
return true;
}
}
class Airship {
private $key = '';
private $secret = '';
public function __construct($key, $secret) {
$this->key = $key;
$this->secret = $secret;
return true;
}
public function _request($url, $method, $body, $content_type=null) {
$rest = new RESTClient($this->key, $this->secret, $content_type);
$rest->createRequest($url, $method, $body);
$rest->sendRequest();
$response = $rest->getResponse();
if ($response[0] == 401) {
throw new Unauthorized();
}
return $response;
}
// Register the device token with UA.
public function register($device_token, $alias=null, $tags=null, $badge=null) {
$url = DEVICE_TOKEN_URL . $device_token;
$payload = array();
if ($alias != null) {
$payload['alias'] = $alias;
}
if ($tags != null) {
$payload['tags'] = $tags;
}
if ($badge != null) {
$payload['badge'] = $badge;
}
if (count($payload) != 0) {
$body = json_encode($payload);
$content_type = 'application/json';
} else {
$body = '';
$content_type = null;
}
$response = $this->_request($url, 'PUT', $body, $content_type);
$response_code = $response[0];
if ($response_code != 201 && $response_code != 200) {
throw new AirshipFailure($response[1], $response_code);
}
return ($response_code == 201);
}
// Mark the device token as inactive.
public function deregister($device_token) {
$url = DEVICE_TOKEN_URL . $device_token;
$response = $this->_request($url, 'DELETE', null, null);
$response_code = $response[0];
if ($response_code != 204) {
throw new AirshipFailure($response[1], $response_code);
}
}
// Retrieve information about this device token.
public function get_device_token_info($device_token) {
$url = DEVICE_TOKEN_URL . $device_token;
$response = $this->_request($url, 'GET', null, null);
$response_code = $response[0];
if ($response_code != 200) {
throw new AirshipFailure($response[1], $response_code);
}
return json_decode($response[1]);
}
public function get_device_tokens() {
return new AirshipDeviceList($this);
}
// Push this payload to the specified device tokens and tags.
public function push($payload, $device_tokens=null, $aliases=null, $tags=null) {
if ($device_tokens != null) {
$payload['device_tokens'] = $device_tokens;
}
if ($aliases != null) {
$payload['aliases'] = $aliases;
}
if ($tags != null) {
$payload['tags'] = $tags;
}
$body = json_encode($payload);
$response = $this->_request(PUSH_URL, 'POST', $body, 'application/json');
$response_code = $response[0];
if ($response_code != 200) {
throw new AirshipFailure($response[1], $response_code);
}
}
// Broadcast this payload to all users.
public function broadcast($payload, $exclude_tokens=null) {
if ($exclude_tokens != null) {
$payload['exclude_tokens'] = $exclude_tokens;
}
$body = json_encode($payload);
$response = $this->_request(BROADCAST_URL, 'POST', $body, 'application/json');
$response_code = $response[0];
if ($response_code != 200) {
throw new AirshipFailure($response[1], $response_code);
}
}
/*
Return device tokens marked as inactive since this timestamp
Return a list of (device token, timestamp, alias) functions.
*/
public function feedback($since) {
$url = FEEDBACK_URL . '?' . 'since=' . rawurlencode($since->format('c'));
$response = $this->_request($url, 'GET', null, null);
$response_code = $response[0];
if ($response_code != 200) {
throw new AirshipFailure($response[1], $response_code);
}
$results = json_decode($response[1]);
foreach ($results as $item) {
$item->marked_inactive_on = new DateTime($item->marked_inactive_on,
new DateTimeZone('UTC'));
}
return $results;
}
}
?>