实现聊天,首先要知道聊天双方的CID,这里可以获取到,将其存起来。
这里用模拟器测试一下,准备俩台模拟器,分别获得其CID;准备好聊天界面;注备好聊天接口,就可以测试了。
一、web后台Contraller
package com.smxy.office.controller;
@Controller
public class AppPush {
//定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
private static String appId = "";
private static String appKey = "";
private static String masterSecret = "";
private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
//这里先写死
private static String CID = "e9ef48ef0ef7155580995aa3b8410d93";
//聊天接口-----ChatUser(cid,chatContentString)
@RequestMapping("/pushMessage")
public void pushMessage( @RequestBody ChatUser chatUser) throws Exception{
String cid = chatUser.getCid();
String msg = chatUser.getMsg();
IIGtPush push = new IGtPush(url, appKey, masterSecret);
IBatch batch = push.getBatch();
System.out.println("CID="+cid+",msg="+msg);
try {
//构建客户a的透传消息a
constructClientTransMsg(cid,msg,batch);
} catch (Exception e) {
e.printStackTrace();
}
batch.submit();
}
private static void constructClientTransMsg(String cid, String msg ,IBatch batch) throws Exception {
SingleMessage message = new SingleMessage();
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionContent(msg);
template.setTransmissionType(0); // 这个Type为int型,填写1则自动启动app
message.setData(template);
message.setOffline(true);
message.setOfflineExpireTime(1 * 1000);
// 设置推送目标,填入appid和clientId
Target target = new Target();
target.setAppId(appId);
target.setClientId(cid);
batch.add(message, target);
}
}
二、App聊天界面
public class MessageFragment extends BaseFragement {
ChatUser chatUser;
public static String URL = "http://lj1757620885.6655.la:54746/office/pushMessage?cid=";
private SharedPreferencesHelper sharedPreferencesHelper;
private String CID,sendMesageContent;//好友的CID
private List<Message> msgList = new ArrayList<>();
@BindView(R.id.message_text) EditText inputText;
@BindView(R.id.msg_recycler_view) RecyclerView msgRecyclerView;
private MsgAdapter msgAdapter;
private DemoIntentService msgService;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.memo_lists, null);
ButterKnife.bind(this,view);
Intent intent = new Intent(getActivity(), DemoIntentService.class);
getActivity().bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
initMsgs();//初始化聊天界面
inputText = view.findViewById(R.id.message_text);//聊天输入框
msgRecyclerView = view.findViewById(R.id.msg_recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
msgRecyclerView.setLayoutManager(layoutManager);
msgAdapter = new MsgAdapter(msgList);
msgRecyclerView.setAdapter(msgAdapter);
return view;
}
private void initMsgs() {
//chatUser = new ChatUser();
sharedPreferencesHelper = new SharedPreferencesHelper(getContext(),"push_CID");
CID = (String) sharedPreferencesHelper.getSharedPreference("cid",null);
MyLog.e("CID==="+CID);
Message msg1 = new Message("物一群的邀请码是多少", Message.TYPE_RECEIVED);
msgList.add(msg1);
Message msg2 = new Message("0X897888V", Message.TYPE_SENT);
msgList.add(msg2);
Message msg3 = new Message("谢谢你", Message.TYPE_RECEIVED);
msgList.add(msg3);
}
//发送按钮点击事件,将聊天内容传给好友
@OnClick(R.id.message_send)
public void sedMessage(){
sendMesageContent = inputText.getText().toString();
chatUser = new ChatUser();
chatUser.setCid("fb61f569efd33cf3eb536eb945537acb");
chatUser.setMsg(sendMesageContent);
if (!"".equals(sendMesageContent)) {
Message msg = new Message(sendMesageContent, Message.TYPE_SENT);
msgList.add(msg);
//当有新消息时,调用适配器notifyItemInserted通知列表有新的数据插入,刷新RecyclerView
msgAdapter.notifyItemInserted(msgList.size() - 1);
//将RecyclerView定位到最后一行,保证可以看到最新消息
msgRecyclerView.scrollToPosition(msgList.size() - 1);
inputText.setText("");
Gson gson = new Gson();
String json = gson.toJson(chatUser);
HttpUtil.getCall("/pushMessage",json).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
}
});
}
}
ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
msgService = ((DemoIntentService.MyGeTui) iBinder).get();
msgService.setOnProgressListener(new DemoIntentService.OnProgressListener() {
@Override
public void onProgress(String progress) {
// mProgressBar.setProgress(progress);
android.os.Message message = android.os.Message.obtain();
message.obj = progress;
mHandler.sendMessage(message);
}
});
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
private Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(android.os.Message message) {
Message msg = new Message((String) message.obj, Message.TYPE_RECEIVED);//接收者
msgList.add(msg);
//当有新消息时,调用适配器notifyItemInserted通知列表有新的数据插入,刷新RecyclerView
msgAdapter.notifyItemInserted(msgList.size() - 1);
//将RecyclerView定位到最后一行,保证可以看到最新消息
msgRecyclerView.scrollToPosition(msgList.size() - 1);
return true;
}
});
}
三、效果图