AcceptHandler.java
8.3 KB
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
package com.glsx.pushsdk.biz;
import android.text.TextUtils;
import com.glsx.commonres.util.DateUtils;
import com.blankj.utilcode.util.LogUtils;
import com.glsx.pushsdk.common.CmdContants;
import com.glsx.pushsdk.common.DecoderData;
import com.glsx.pushsdk.common.NetStatu;
import com.glsx.pushsdk.common.Param;
import com.glsx.pushsdk.common.PushCommon;
import com.glsx.pushsdk.manager.DataManager;
import com.glsx.pushsdk.manager.GLNotifiactionManager;
import com.glsx.pushsdk.manager.GLPushManager;
import com.glsx.pushsdk.model.Entry;
import com.glsx.pushsdk.model.ForceExitEntry;
import com.glsx.pushsdk.model.GetOffLineContentItem;
import com.glsx.pushsdk.model.GetOfflLineContentDataResult;
import com.glsx.pushsdk.model.GetOfflLineDataResult;
import com.glsx.pushsdk.model.Protocol;
import com.glsx.pushsdk.model.SdkVersionResult;
import com.glsx.pushsdk.tools.ProtocolHandler;
import com.glsx.pushsdk.tools.Tools;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
/**
* 接收handler,广联接入平台推送消息接收处理
*
* @author YFZX-yulg
*/
public class AcceptHandler extends ChannelInboundHandlerAdapter {
public static final String TAG = "AcceptHandler";
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg == null) {
LogUtils.w(TAG, "channelRead, msg is null!");
return;
}
Protocol protocol;
if (msg instanceof Protocol) {
protocol = (Protocol) msg;
} else {
protocol = ProtocolHandler.parseProtocol((byte[]) msg);
}
if (protocol == null || protocol.getData() == null) {
LogUtils.w(TAG, "channelRead, protocol is null or protocol.getData() is null");
return;
}
short protocolCmdId = protocol.getCmdId();
// LogUtils.w(TAG, "channelRead, protocol.getCmdId() =" + protocolCmdId);
switch (protocolCmdId) {
case CmdContants.LOGIN_BACK: {//登录成功之后,获取一次离线消息
GLPushManager.getInstance().buildGetOffLineData();
}
break;
case CmdContants.GET_OFF_LINE_BACK: {// 获取离线(返回离线消息ID)返回
LogUtils.w(TAG, "获取离线数据成功");
GetOfflLineDataResult result = DecoderData.getOffLineData(protocol.getData());
if (result.getCode() == CmdContants.SUCCESS) {
LogUtils.w(TAG, "离线数据 条数 count = " + result.getCount());
for (int i = 0; i < result.getmListMsgId().size(); i++) {
LogUtils.w(TAG, "离线数据 条数 id = " + result.getmListMsgId().get(i).getMsgId());
}
DataManager.getInstance().setmGetOfflLineDataResult(result);
GLPushManager.getInstance().buildGetOffLineDetailData();
}
break;
}
case CmdContants.GET_OFF_LINE_DETAIL_MESSAGE_BACK: {// 根据消息ID获取消息内容返回
GetOfflLineContentDataResult result = DecoderData.getOffLineContextData(protocol.getData());
if (result.getCode() == CmdContants.SUCCESS) {
LogUtils.w(TAG, "获取到离线内容数据= ");
for (int i = 0; i < result.getmList().size(); i++) {
LogUtils.w(TAG, "离线数据内容 = " + result.getmList().get(i).getMsgContext());
GLNotifiactionManager.getInstance().setOffMessageNotification(
result.getmList().get(i).getMsgTitle(),
result.getmList().get(i).getMsgContext(),
result.getmList().get(i).getMsgAttribute());
}
}
break;
}
case CmdContants.EXIT_BACK: {// 请求退出登录
Entry result = DecoderData.getHearBeat(protocol.getData());
if (result.getCode() == CmdContants.SUCCESS) {
LogUtils.w(TAG, "请求退出登录成功");
GLPushManager.getInstance().closeAll();
LogUtils.w(TAG, "开始重新登录,userFlag为空");
GLPushManager.getInstance().setAppUserId("");//退出登录成功,userFlag设置为""
Tools.saveUserFlag(GLPushManager.getInstance().getMyContext(), "");
//重新获取业务服务器的IP和端口
PushCommon.BUSSI_SVR_URL = "";
PushCommon.BUSSI_SVR_port = 0;
NetStatu.busiStatus = NetStatu.START_CONNSERVER;
GLPushManager.getInstance().connectServer();
}
break;
}
case CmdContants.SERVER_PUSH_MESSAGE_SEND: {// 服务器推送消息
GetOffLineContentItem result = DecoderData.getSeviceToClientMsg(protocol.getData());
LogUtils.w(TAG, "服务器推送消息成功");
GLNotifiactionManager.getInstance().setUpNotification(
result.getMsgTitle(),
result.getMsgContext(),
result.getMsgAttribute(),
result.getMsgId());
ctx.writeAndFlush(
Param.buildSendData(
protocol.getSeq(), result.getMsgId(), DateUtils.getTimestampStr()));// 发送数据给服务器,接收到推送消息
break;
}
case CmdContants.GET_VERSION_BACK: {// 发送版本号给服务器
SdkVersionResult result = DecoderData.getSdkVersionData(protocol.getData());
if (result.getCode() == 0) {
LogUtils.w(TAG, "获取SDK版本号" + result.getChVerNo() + ", CMDID = " + protocol.getCmdId());
}
break;
}
case CmdContants.SET_USERID_BACK: {// 重新设置userId反馈信息
Entry result = DecoderData.getHearBeat(protocol.getData());
if (result.getCode() == 0) {
LogUtils.w(TAG, "重新设置UserId成功" + result.getErrMessage());
GLPushManager.getInstance().buildGetOffLineData();//重置UserId之后,重新拉去离线消息
}
break;
}
case CmdContants.SERVER_EXIT_SEND: {//接收到强制退出请求
ForceExitEntry result = DecoderData.getForceExitLogin(protocol.getData());
if (result.getChRstCode() == 0) {
LogUtils.w(TAG, "接收到强制退出请求");
if (!TextUtils.isEmpty(result.getChGlsxKey())
&& !(result.getChGlsxKey()).equals(GLPushManager.getInstance().getGlsxKey())) {
GLPushManager.getInstance().closeAll();
LogUtils.w(TAG, "开始重新登录,userFlag为空");
GLPushManager.getInstance().setAppUserId("");//退出登录成功,userFlag设置为""
Tools.saveUserFlag(GLPushManager.getInstance().getMyContext(), "");
//重新获取业务服务器的IP和端口
PushCommon.BUSSI_SVR_URL = "";
PushCommon.BUSSI_SVR_port = 0;
NetStatu.busiStatus = NetStatu.START_CONNSERVER;
GLPushManager.getInstance().connectServer();
}
}
break;
}
case CmdContants.CLICK_MSG_BACK: {// 终端点击消息通知反馈信息
Entry result = DecoderData.getHearBeat(protocol.getData());
if (result.getCode() == 0) {
LogUtils.w(TAG, "终端点击消息通知成功" + result.getErrMessage());
}
break;
}
default:
break;
}
ctx.fireChannelRead(protocol);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
super.channelReadComplete(ctx);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}