forked from bryglen/yii2-apns-gcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gcm.php
185 lines (167 loc) · 5.34 KB
/
Gcm.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
<?php
/**
* @author Bryan Jayson Tan <[email protected]>
* @link http://bryantan.info
*/
namespace bryglen\apnsgcm;
use yii\base\Component;
use yii\base\InvalidConfigException;
class Gcm extends AbstractApnsGcm
{
public $apiKey;
private $_client = null;
public function init()
{
if (!$this->apiKey) {
throw new InvalidConfigException('Api key cannot be empty');
}
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',
* [
* '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 = [], $args = [])
{
// 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 : [$value];
call_user_func_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->getErrorCode() != null ? false : true;
if (!$this->success) {
$this->errors[] = $result->getErrorCode();
}
// HTTP code 200, but message sent with error
} catch (\InvalidArgumentException $e) {
$this->errors[] = $e->getMessage();
// $deviceRegistrationId was null
} catch (\PHP_GCM\InvalidRequestException $e) {
if ($e->getMessage()) {
$this->errors[] = $e->getMessage();
} else {
$this->errors[] = sprintf("Received error code %s from GCM Service", $e->getCode());
}
// server returned HTTP code other than 200 or 503
} catch (\Exception $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',
* [
* 'custom_data_key_1' => 'custom_data_value_1',
* 'custom_data_key_2' => 'custom_data_value_2',
* ]
* );
* </code>
*
* Usage 2:
* <code>
* $this->sendMulti(
* ['valid-token-1','valid-token-2','valid-token-3'],
* 'some-message',
* [
* '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 = [], $args = [])
{
$tokens = is_array($tokens) ? $tokens : [$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 : [$value];
call_user_func_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) {
if ($e->getMessage()) {
$this->errors[] = $e->getMessage();
} else {
$this->errors[] = sprintf("Received error code %s from GCM Service", $e->getCode());
}
// server returned HTTP code other than 200 or 503
} catch (\Exception $e) {
$this->errors[] = $e->getMessage();
// message could not be sent
}
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);
}
}