|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Support\Tencent; |
|
4
|
|
|
|
|
5
|
|
|
class XgPusher |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* The XingeApp instance. |
|
9
|
|
|
* |
|
10
|
|
|
* @var \XingeApp |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $service; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Create a new instance. |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->service = static::createService(); |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Get the XingeApp instance. |
|
24
|
|
|
* |
|
25
|
|
|
* @return \XingeApp |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getService() |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->service; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Create a XingeApp instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @return \XingeApp |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function createService() |
|
38
|
|
|
{ |
|
39
|
|
|
return new XingeApp(static::appKey(), static::appSecret()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get the app key. |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public static function appKey() |
|
48
|
|
|
{ |
|
49
|
|
|
return config('services.xgpush.key'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the app secret. |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function appSecret() |
|
58
|
|
|
{ |
|
59
|
|
|
return config('services.xgpush.secret'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get the custom key. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function customKey() |
|
68
|
|
|
{ |
|
69
|
|
|
return config('services.xgpush.custom_key', 'custom'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the environment. |
|
74
|
|
|
* |
|
75
|
|
|
* environment: 向iOS设备推送时必填,1表示推送生产环境;2表示推送开发环境。推送Android平台不填或填0. |
|
76
|
|
|
* |
|
77
|
|
|
* @return int |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function environment() |
|
80
|
|
|
{ |
|
81
|
|
|
return config('services.xgpush.environment') == 'production' ? |
|
82
|
|
|
XingeApp::IOSENV_PROD : |
|
83
|
|
|
XingeApp::IOSENV_DEV; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* 解析信鸽返回的结果。 |
|
88
|
|
|
* |
|
89
|
|
|
* @see http://developer.qq.com/wiki/xg/%E6%9C%8D%E5%8A%A1%E7%AB%AFAPI%E6%8E%A5%E5%85%A5/Rest%20API%20%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/Rest%20API%20%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97.html |
|
90
|
|
|
* |
|
91
|
|
|
* @param mixed $xgResult 信鸽的请求结果 |
|
92
|
|
|
* @param int &$code 返回码,0 为成功 |
|
93
|
|
|
* @param string &$message 请求出错时的错误信息 |
|
94
|
|
|
* @param mixed &$result 请求正确时的额外数据 |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function parseResult($xgResult = null, &$code = null, &$message = null, &$result = null) |
|
98
|
|
|
{ |
|
99
|
|
|
if (is_array($xgResult) && isset($xgResult['ret_code'])) { |
|
100
|
|
|
$code = (int) $xgResult['ret_code']; |
|
101
|
|
|
$message = isset($xgResult['err_msg']) ? (string) $xgResult['err_msg'] : ''; |
|
102
|
|
|
if (isset($xgResult['result'])) { |
|
103
|
|
|
$result = $xgResult['result']; |
|
104
|
|
|
} |
|
105
|
|
|
} else { |
|
106
|
|
|
$code = -99999; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $code === 0; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Encode the custom data. |
|
114
|
|
|
* |
|
115
|
|
|
* @param mixed $data |
|
116
|
|
|
* @return array|null |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function encodeCustomData($data) |
|
119
|
|
|
{ |
|
120
|
|
|
return ! empty($data) ? [static::customKey() => $data] : null; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get Xinge account for the given user. |
|
125
|
|
|
* |
|
126
|
|
|
* @param mixed $user |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public static function accountForUser($user) |
|
130
|
|
|
{ |
|
131
|
|
View Code Duplication |
if (is_object($user)) { |
|
|
|
|
|
|
132
|
|
|
$user = $user->id; |
|
133
|
|
|
} elseif (is_array($user)) { |
|
134
|
|
|
$user = $user['id']; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// 信鸽不允许使用简单的账号,例如纯数字的id。 |
|
138
|
|
|
// 所以在 userId 前面加个 'user' 字符。 |
|
139
|
|
|
if (starts_with($user, 'user')) { |
|
140
|
|
|
return $user; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return 'user'.$user; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Creates a MessageIOS instance. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $alert |
|
150
|
|
|
* @param mixed $custom |
|
151
|
|
|
* @param int $badge |
|
152
|
|
|
* @param string $sound |
|
153
|
|
|
* @return MessageIOS |
|
154
|
|
|
*/ |
|
155
|
|
|
public static function createIOSMessage($alert = '', $custom = null, $badge = 1, $sound = 'default') |
|
156
|
|
|
{ |
|
157
|
|
|
$message = new MessageIOS(); |
|
158
|
|
|
$message->setAlert($alert); |
|
159
|
|
|
if ($customData = static::encodeCustomData($custom)) { |
|
160
|
|
|
$message->setCustom($customData); |
|
161
|
|
|
} |
|
162
|
|
|
if (is_numeric($badge) && $badge >= 0) { |
|
163
|
|
|
$message->setBadge($badge); |
|
164
|
|
|
} |
|
165
|
|
|
if (! empty($sound)) { |
|
166
|
|
|
$message->setSound($sound); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $message; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Create a Message instance. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $content |
|
176
|
|
|
* @param mixed $custom |
|
177
|
|
|
* @param string $title |
|
178
|
|
|
* @param int $type |
|
179
|
|
|
* @return Message |
|
180
|
|
|
*/ |
|
181
|
|
|
public static function createAndroidMessage($content = '', $custom = null, $title = null, $type = Message::TYPE_NOTIFICATION) |
|
182
|
|
|
{ |
|
183
|
|
|
$message = new Message(); |
|
184
|
|
|
$message->setTitle($title ?: config('app.name')); |
|
185
|
|
|
$message->setContent($content); |
|
186
|
|
|
if ($customData = static::encodeCustomData($custom)) { |
|
187
|
|
|
$message->setCustom($customData); |
|
188
|
|
|
} |
|
189
|
|
|
$message->setType($type); |
|
190
|
|
|
//含义:样式编号0,响铃,震动,不可从通知栏清除,不影响先前通知 |
|
191
|
|
|
$message->setStyle(new Style(0, 1, 1, 1, 0)); |
|
192
|
|
|
$action = new ClickAction(); |
|
193
|
|
|
$action->setActionType(ClickAction::TYPE_ACTIVITY); |
|
194
|
|
|
$message->setAction($action); |
|
195
|
|
|
|
|
196
|
|
|
return $message; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Query all device tokens for the given user. |
|
201
|
|
|
* |
|
202
|
|
|
* @param mixed $user |
|
203
|
|
|
* @return string[]|null |
|
204
|
|
|
*/ |
|
205
|
|
View Code Duplication |
public static function queryDeviceTokensForUser($user) |
|
|
|
|
|
|
206
|
|
|
{ |
|
207
|
|
|
$query = static::createService()->QueryTokensOfAccount(static::accountForUser($user)); |
|
208
|
|
|
|
|
209
|
|
|
if (static::parseResult($query, null, null, $result)) { |
|
|
|
|
|
|
210
|
|
|
return is_array($result) ? array_get($result, 'tokens', []) : []; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Query all tags for the given device token. |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $deviceToken |
|
218
|
|
|
* @return string[]|null |
|
219
|
|
|
*/ |
|
220
|
|
View Code Duplication |
public static function queryTagsForDeviceToken($deviceToken) |
|
|
|
|
|
|
221
|
|
|
{ |
|
222
|
|
|
$query = static::createService()->QueryTokenTags($deviceToken); |
|
223
|
|
|
|
|
224
|
|
|
if (static::parseResult($query, null, null, $result)) { |
|
|
|
|
|
|
225
|
|
|
return is_array($result) ? array_get($result, 'tags', []) : []; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Query all tags for the given user. |
|
231
|
|
|
* |
|
232
|
|
|
* @param mixed $user |
|
233
|
|
|
* @param array &$deviceTokens |
|
234
|
|
|
* @return array|null |
|
235
|
|
|
*/ |
|
236
|
|
|
public static function queryTagsForUser($user, &$deviceTokens = null) |
|
237
|
|
|
{ |
|
238
|
|
|
$deviceTokens = static::queryDeviceTokensForUser($user); |
|
239
|
|
|
|
|
240
|
|
|
$result = []; |
|
241
|
|
|
foreach ($deviceTokens as $token) { |
|
|
|
|
|
|
242
|
|
|
if ($tags = static::queryTagsForDeviceToken($token)) { |
|
243
|
|
|
$result[$token] = $tags; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return $result; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..