-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathACSPushNotification.php
More file actions
180 lines (171 loc) · 4.37 KB
/
ACSPushNotification.php
File metadata and controls
180 lines (171 loc) · 4.37 KB
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
<?php
/**
* Appcelerator Cloud Service PHP Push Notification
*
* Send push notification from a PHP application to ACS
*
* Require: cURL, write access to temp folder
*/
class ACSPushNotification {
protected $appKey = '';
protected $adminName = '';
protected $adminPass = '';
protected $vibrate = false;
protected $log = array();
protected $path = '';
protected $cookieJar = '';
protected $ch;
protected $errorCode = 0;
protected $maxRetry = 5;
protected $retryDelay = 1;
/**
* @param array $options
*/
public function __construct($options) {
$this->path = dirname(__FILE__);
$this->cookieJar = sys_get_temp_dir() . '/cookie.txt';
$this->appKey = $options['appKey'];
$this->adminName = $options['adminName'];
$this->adminPass = $options['adminPass'];
if (isset($options['vibrate'])) {
$this->vibrate = $options['vibrate'];
}
$this->initRequest();
}
/**
* @return array
*/
public function getLog() {
return $this->log;
}
/**
* @param string $title
* @param string $message
* @param string $channel
* @param int $retry
*/
public function send($title, $message, $channel, $retry=0) {
if ($retry < $this->maxRetry) {
if ($retry > 0) {
sleep($this->retryDelay * $retry);
}
$result = $this->sendRequest('https://api.cloud.appcelerator.com/v1/push_notification/notify.json', array(
'channel' => $channel,
'payload' => json_encode(array(
'badge' => 1,
'sound' => 'default',
'alert' => $message,
'title' => $title,
'icon' => 'appicon',
'vibrate' => $this->vibrate
))
));
if (!$result) {
$r = false;
if ($this->errorCode == 401) { // not logged in
$r = $this->loginAdmin();
}
if ($this->errorCode == 404) {
$z = @unlink($this->cookieJar);
$this->log[] = 'delete cookieJar = '. $z;
$r = $this->loginAdmin();
}
if ($r) {
$this->send($title, $message, $channel, ++$retry);
}
}
}
$this->log[] = 'send(), retry = '. $retry .', result = ';
$this->log[] = $result;
}
/**
* close curl handler
*/
public function close() {
if ($this->ch) {
curl_close($this->ch);
$this->ch = null;
}
}
/**
* POST https://api.cloud.appcelerator.com/v1/users/login.json
* - login
* - password
* @param int $retry
*/
protected function loginAdmin($retry=0) {
if ($retry < $this->maxRetry) {
if ($retry > 0) {
sleep($this->retryDelay * $retry);
}
$result = $this->sendRequest('https://api.cloud.appcelerator.com/v1/users/login.json', array(
'login' => $this->adminName,
'password' => $this->adminPass
));
$this->log[] = 'loginAdmin(), retry = '. $retry .', result = ';
$this->log[] = $result;
if ($result) {
return true;
} else {
if ($this->errorCode == 404) {
$r = @unlink($this->cookieJar);
$this->log[] = 'delete cookieJar = '. $r;
}
return $this->loginAdmin(++$retry);
}
}
return false;
}
/**
* initialize curl handler
*/
protected function initRequest() {
if ($this->ch === null) {
$this->ch = curl_init();
$opt = array(
CURLOPT_TIMEOUT => 60,
// set ssl certificate
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_CAINFO => $this->path . '/cacert.pem',
// set post parameter
CURLOPT_POST => true,
// store cookie
CURLOPT_COOKIEFILE => $this->cookieJar,
CURLOPT_COOKIEJAR => $this->cookieJar,
// return result as string
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($this->ch, $opt);
}
}
/**
* @param string $url
* @param array $postfields
* @return boolean
*/
protected function sendRequest($url, $postfields) {
$this->errorCode = 0;
$url = $url . '?key=' . $this->appKey;
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
$response = curl_exec($this->ch);
$this->log[] = 'curl_error = '; $this->log[] = curl_error($this->ch);
$this->log[] = 'curl_errno = '; $this->log[] = curl_errno($this->ch);
$this->log[] = 'sendRequest(), response = ';
$this->log[] = $response;
$json = json_decode($response, true);
$this->log[] = 'sendRequest(), json = ';
$this->log[] = $json;
if (is_array($json) && isset($json['meta'])) {
if ($json['meta']['code'] == 200) { // successful
return true;
}
$this->errorCode = $json['meta']['code'];
} else {
$this->errorCode = 500;
}
return false;
}
}
?>