Android Studio-個推-實現簡單聊天(二)
阿新 • • 發佈:2018-12-16
實現聊天,首先要知道聊天雙方的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; } }); }
三、效果圖