-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathYiiApns.php
236 lines (208 loc) · 7.17 KB
/
YiiApns.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
<?php
define('AAPNS_LIB_PATH', dirname(__FILE__) . '/apns');
require_once dirname(__FILE__) . '/YiiApnsGcmBase.php';
/**
* Class YiiApns
*
* @method YiiApns add(ApnsPHP_Message $message)
* @method YiiApns getQueue($bEmpty = true)
* @method YiiApns getErrors($bEmpty = true)
* @method YiiApns getSendRetryTimes()
* @method YiiApns setSendRetryTimes($nRetryTimes)
* @method YiiApns disconnect()
* @method YiiApns connect()
* @method YiiApns getSocketSelectTimeout()
* @method YiiApns setSocketSelectTimeout($nSelectTimeout)
*/
class YiiApns extends YiiApnsGcmBase
{
const ENVIRONMENT_SANDBOX = 'sandbox';
const ENVIRONMENT_PRODUCTION = 'production';
private $_client = null;
public $environment;
public $pemFile;
/**
* additional information for the push provider
* @var array
*/
public $options = array();
public $logger = 'YiiApnsLog';
public function init()
{
if (!in_array($this->environment, array(self::ENVIRONMENT_SANDBOX, self::ENVIRONMENT_PRODUCTION))) {
throw new CException('Environment is invalid.');
}
if (!$this->pemFile || !file_exists($this->pemFile)) {
throw new CException('Push SSL certificate is required.');
}
$this->initAutoloader();
require_once dirname(__FILE__) . '/YiiApnsLog.php';
Yii::app()->attachEventHandler('onEndRequest', array($this, 'onApplicationEndRequest'));
parent::init();
}
/**
* @return ApnsPHP_Push|null
*/
public function getClient()
{
if ($this->_client === null) {
$this->_client = new ApnsPHP_Push(
$this->environment == self::ENVIRONMENT_PRODUCTION ? ApnsPHP_Push::ENVIRONMENT_PRODUCTION : ApnsPHP_Push::ENVIRONMENT_SANDBOX,
$this->pemFile
);
$this->options['logger'] = new $this->logger;
if ($this->retryTimes) {
$this->options['sendRetryTimes'] = $this->retryTimes;
}
foreach ($this->options as $key => $value) {
$method = 'set' . ucfirst($key);
$value = is_array($value) ? $value : array($value);
call_user_func_array(array($this->_client, $method), $value);
}
$this->_client->connect();
}
return $this->_client;
}
/**
* send a push notification for ios using APNS client
*
* Usage 1:
* <code>
* $this->send('some-valid-token','some-message',
* array(
* 'custom_data_key_1'=>'custom_data_value_1',
* 'custom_data_key_2'=>'custom_data_value_2',
* ),
* array(
* 'badge'=>2,
* 'expiry'=>30
* 'sound'=>'default',
* )
* );
* </code>
* @param string $token
* @param string $text a message in sending push notification
* @param array $payloadData The payload contains information about how the system should alert the user as well as any custom data you provide
* @param array $args optional additional information in sending a message
* @return ApnsPHP_Message|null
* @tutorial https://github.com/duccio/ApnsPHP
*/
public function send($token, $text, $payloadData = array(), $args = array())
{
// check if its dry run or not
if ($this->dryRun === true) {
$this->log($token, $text, $payloadData, $args = array());
$this->success = true;
return null;
}
$message = new ApnsPHP_Message($token);
$message->setText($text);
foreach($args as $method => $value) {
if (strpos($method, 'set') === false) {
$method = 'set' . ucfirst($method);
}
$value = is_array($value) ? $value : array($value);
call_user_func_array(array($message, $method), $value);
}
// set a custom payload data
foreach ($payloadData as $key => $value) {
$message->setCustomProperty($key, $value);
}
// Add the message to the message queue
$this->add($message);
// send a message
$this->getClient()->send();
$this->errors = $this->getClient()->getErrors();
$this->success = $this->getClient()->getErrors() ? true : false;
return $message;
}
/**
* @param array|string $tokens
* @param $text
* @param array $payloadData
* @param array $args
* @return ApnsPHP_Message|null
*/
public function sendMulti($tokens, $text, $payloadData = array(), $args = array())
{
$tokens = is_array($tokens) ? $tokens : array($tokens);
// check if its dry run or not
if ($this->dryRun === true) {
$this->log($tokens, $text, $payloadData, $args = array());
return null;
}
$message = new ApnsPHP_Message();
foreach ($tokens as $token) {
$message->addRecipient($token);
}
$message->setText($text);
foreach($args as $method => $value) {
if (strpos($method, 'set') === false) {
$method = 'set' . ucfirst($method);
}
$value = is_array($value) ? $value : array($value);
call_user_func_array(array($message, $method), $value);
}
// set a custom payload data
foreach ($payloadData as $key => $value) {
$message->setCustomProperty($key, $value);
}
// Add the message to the message queue
$this->add($message);
// send a message
$this->getClient()->send();
$this->errors = $this->getClient()->getErrors();
$this->success = $this->getClient()->getErrors() ? true : false;
return $message;
}
/**
* Disconnect push provider on app exit.
* @param CEvent $event
*/
public function onApplicationEndRequest(CEvent $event)
{
//echo 'disconnecting'; die();
if ($this->getClient())
$this->getClient()->disconnect();
}
/**
*
*/
public function initAutoloader()
{
Yii::registerAutoloader(array(__CLASS__, 'autoload'));
}
/**
* A modified version of ApnsPHP_Autoload tailored to work with Yii. There's generally no
* need to register or use this directly.
*
* This autoloader fails if initAutoloader is not called first or AAPNS_LIB_PATH is not defined.
*
* @param string $className
*/
public static function autoload($className)
{
//if a long name separated by `\` (i.e. with namespace). get only last part.
$temp = explode('\\', $className);
$className = end($temp);
if (!defined('AAPNS_LIB_PATH'))
return;
if (empty($className) || strpos($className, 'ApnsPHP_') !== 0)
return;
$filePath = sprintf('%s%s%s.php',
AAPNS_LIB_PATH, DIRECTORY_SEPARATOR,
str_replace('_', DIRECTORY_SEPARATOR, str_replace('ApnsPHP_', '', $className))
);
if (!is_file($filePath) || !is_readable($filePath)) {
return; // let Yii handle this
}
require_once($filePath);
}
public function __call($method, $params)
{
$client = $this->getClient();
if (method_exists($client, $method))
return call_user_func_array(array($client, $method), $params);
return parent::__call($method, $params);
}
}