-
Notifications
You must be signed in to change notification settings - Fork 39
/
Apns.php
267 lines (234 loc) · 7.45 KB
/
Apns.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
<?php
/**
* @author Bryan Jayson Tan <[email protected]>
* @link http://bryantan.info
*/
namespace bryglen\apnsgcm;
use Yii;
use yii\base\Application;
use yii\base\Component;
use yii\base\InvalidConfigException;
/**
* Class Apns
*
* @method Apns add(\ApnsPHP_Message $message)
* @method Apns getQueue($bEmpty = true)
* @method Apns getErrors($bEmpty = true)
* @method Apns getSendRetryTimes()
* @method Apns setSendRetryTimes($nRetryTimes)
* @method Apns disconnect()
* @method Apns connect()
* @method Apns getSocketSelectTimeout()
* @method Apns setSocketSelectTimeout($nSelectTimeout)
*
* @package bryglen\apnsgcm
*/
class Apns extends AbstractApnsGcm
{
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 = [];
public $logger = 'bryglen\apnsgcm\ApnsLog';
public function init()
{
}
public function setEnvironment($environment)
{
if ($environment) {
$this->environment = $environment;
return;
}
return -1;
}
public function setPemFile($pemFile)
{
if ($pemFile) {
$this->pemFile = $pemFile;
return;
}
return -1;
}
public function setOptions($options)
{
if ($options && is_array($options)) {
$this->options = $options;
return;
}
return -1;
}
public function __init($pemFile = null, $options = null, $environment = null)
{
$this->setEnvironment($environment);
$this->setPemFile($pemFile);
$this->setOptions($options);
if (!in_array($this->environment, [self::ENVIRONMENT_SANDBOX, self::ENVIRONMENT_PRODUCTION])) {
throw new InvalidConfigException('Environment is invalid.');
}
if (!$this->pemFile || !file_exists($this->pemFile)) {
throw new InvalidConfigException('Invalid Pem file');
}
Yii::$app->on(
Application::EVENT_AFTER_REQUEST,
function ($event) {
if ($this->getClient()) {
$this->getClient()->disconnect();
}
}
);
}
public function closeConnection()
{
}
/**
* @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 : [$value];
call_user_func_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',
* [
* 'custom_data_key_1'=>'custom_data_value_1',
* 'custom_data_key_2'=>'custom_data_value_2',
* ],
* [
* '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 = [], $args = [])
{
// check if its dry run or not
if ($this->dryRun === true) {
$this->log($token, $text, $payloadData, $args);
$this->success = true;
return null;
}
if (is_array($text)) {
$message = new \ApnsPHP_Message_Custom($token);
if (isset($text['title'])) {
$message->setTitle($text['title']);
}
if (isset($text['body'])) {
$message->setText($text['body']);
}
} else {
$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 : [$value];
call_user_func_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 = empty($this->errors) ? 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 = [], $args = [])
{
$tokens = is_array($tokens) ? $tokens : [$tokens];
// check if its dry run or not
if ($this->dryRun === true) {
$this->log($tokens, $text, $payloadData, $args = []);
return null;
}
if (is_array($text)) {
$message = new \ApnsPHP_Message_Custom();
if (isset($text['title'])) {
$message->setTitle($text['title']);
}
if (isset($text['body'])) {
$message->setText($text['body']);
}
} else {
$message = new \ApnsPHP_Message();
$message->setText($text);
}
foreach ($tokens as $token) {
$message->addRecipient($token);
}
foreach ($args as $method => $value) {
if (strpos($message, 'set') === false) {
$method = 'set' . ucfirst($method);
}
$value = is_array($value) ? $value : [$value];
call_user_func_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 = empty($this->errors) ? true : false;
return $message;
}
public function __call($method, $params)
{
$client = $this->getClient();
if (method_exists($client, $method)) {
return call_user_func_array([$client, $method], $params);
}
return parent::__call($method, $params);
}
}