forked from bryglen/yii-apns-gcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YiiGcm.php
172 lines (152 loc) · 4.99 KB
/
YiiGcm.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
<?php
require_once(dirname(__FILE__) . '/YiiApnsGcmBase.php');
define('YII_GCM_LIB_PATH', dirname(__FILE__) . '/lib');
class YiiGCM extends YiiApnsGcmBase
{
public $apiKey;
private $_client = null;
public function init()
{
if (!$this->apiKey) {
throw new CException('Api key cannot be empty');
}
$this->initAutoloader();
parent::init();
}
public function getClient()
{
if ($this->_client === null) {
$this->_client = new \PHP_GCM\Sender($this->apiKey);
}
return $this->_client;
}
/**
* send a push notification for android using GCM 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',
* ));
* </code>
* @param string $token
* @param $text
* @param array $payloadData
* @param array $args
* @return null|\PHP_GCM\Message
*/
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);
return null;
}
$message = new PHP_GCM\Message();
foreach($args as $method => $value) {
$value = is_array($value) ? $value : array($value);
call_user_func_array(array($message, $method), $value);
}
// set a custom payload data
$payloadData['message'] = $text;
foreach($payloadData as $key=>$value){
$message->addData($key,$value);
}
try {
// send a message
$result = $this->getClient()->send($message, $token, $this->retryTimes);
$this->success = $result ? true : false;
} catch (InvalidArgumentException $e) {
$this->errors[] = $e->getMessage();
// $deviceRegistrationId was null
} catch (PHP_GCM\InvalidRequestException $e) {
$this->errors[] = $e->getMessage();
// server returned HTTP code other than 200 or 503
} catch (CException $e) {
$this->errors[] = $e->getMessage();
// message could not be sent
}
return $message;
}
/**
* send a push notification for android using GCM client
*
* Usage 1:
* <code>
* $this->sendMulti('some-valid-token','some-message',
* array(
* 'custom_data_key_1'=>'custom_data_value_1',
* 'custom_data_key_2'=>'custom_data_value_2',
* ));
* </code>
*
* Usage 2:
* <code>
* $this->sendMulti(array('valid-token-1','valid-token-2','valid-token-3'),'some-message',
* array(
* 'custom_data_key_1'=>'custom_data_value_1',
* 'custom_data_key_2'=>'custom_data_value_2',
* ));
* </code>
* @param string|array $tokens
* @param $text
* @param array $payloadData
* @param array $args
* @return null|\PHP_GCM\Message
*/
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);
$this->success = true;
return null;
}
$message = new PHP_GCM\Message();
foreach($args as $method => $value) {
$value = is_array($value) ? $value : array($value);
call_user_func_array(array($message, $method), $value);
}
// set a custom payload data
$payloadData['message'] = $text;
foreach($payloadData as $key=>$value){
$message->addData($key,$value);
}
try {
// send a message
$result = $this->getClient()->sendMulti($message, $tokens, $this->retryTimes);
$this->success = $result->getSuccess();;
} catch (InvalidArgumentException $e) {
$this->errors[] = $e->getMessage();
// $deviceRegistrationId was null
} catch (PHP_GCM\InvalidRequestException $e) {
$this->errors[] = $e->getMessage();
// server returned HTTP code other than 200 or 503
} catch (CException $e) {
$this->errors[] = $e->getMessage();
// message could not be sent
}
return $message;
}
/**
* auto load all files under gcm folder
*/
public function initAutoloader()
{
$dir = dirname(__FILE__).'/gcm';
$files = glob( $dir . '/*.php' );
foreach ( $files as $file ) {
require_once( $file );
}
}
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);
}
}