-
Notifications
You must be signed in to change notification settings - Fork 1
/
IGtBatch.php
executable file
·90 lines (81 loc) · 2.96 KB
/
IGtBatch.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
<?php
namespace wh\getui;
class IGtBatch
{
var $batchId;
var $innerMsgList = array();
var $seqId = 0;
var $APPKEY;
var $push;
var $lastPostData;
public function __construct($appkey, $push)
{
$this->APPKEY = $appkey;
$this->push = $push;
$this->batchId = uniqid();
}
public function getBatchId()
{
return $this->batchId;
}
public function add($message, $target)
{
if ($this->seqId >= 5000) {
throw new Exception("Can not add over 5000 message once! Please call submit() first.");
} else {
$this->seqId += 1;
$innerMsg = new SingleBatchItem();
$innerMsg->set_seqId($this->seqId);
$innerMsg->set_data($this->createSingleJson($message, $target));
array_push($this->innerMsgList, $innerMsg);
}
return $this->seqId . "";
}
public function createSingleJson($message, $target)
{
$params = array();
$params["action"] = "pushMessageToSingleAction";
$params["appkey"] = $this->APPKEY;
$params["clientData"] = base64_encode($message->get_data()->get_transparent());
$params["transmissionContent"] = $message->get_data()->get_transmissionContent();
$params["isOffline"] = $message->get_isOffline();
$params["offlineExpireTime"] = $message->get_offlineExpireTime();
// 增加pushNetWorkType参数(0:不限;1:wifi;2:4G/3G/2G)
$params["pushNetWorkType"] = $message->get_pushNetWorkType();
$params["appId"] = $target->get_appId();
$params["clientId"] = $target->get_clientId();
$params["alias"] = $target->get_alias();
// 默认都为消息
$params["type"] = 2;
$params["pushType"] = $message->get_data()->get_pushType();
return json_encode($params);
}
public function submit()
{
$requestId = uniqid();
$data = array();
$data["appkey"]=$this->APPKEY;
$data["serialize"] = "pb";
$data["async"] = GTConfig::isPushSingleBatchAsync();
$data["action"] = "pushMessageToSingleBatchAction";
$singleBatchRequest = new SingleBatchRequest();
$singleBatchRequest->set_batchId($this->batchId);
foreach ($this->innerMsgList as $index => $innerMsg) {
$singleBatchRequest->add_batchItem();
$singleBatchRequest->set_batchItem($index, $innerMsg);
}
$data["singleDatas"] = base64_encode($singleBatchRequest->SerializeToString());
$this->seqId = 0;
$this->innerMsgList = array();
$this->lastPostData = $data;
$result = $this->push->httpPostJSON(null, $data, true);
return $result;
}
public function retry()
{
$result = $this->push->httpPostJSON(null, $this->lastPostData, true);
return $result;
}
public function setApiUrl($apiUrl) {
}
}