我们APP之间创建的会话可唯一,如何与java后台创建的会话是同一个
这是app创建会话的方法
client.createConversation(Arrays.asList(chatId), chatName, null, false, true, new AVIMConversationCreatedCallback() {
@Override
public void done(final AVIMConversation conversation, AVIMException e) {
if (e == null) {
Chat2Activity.this.conversation = conversation;
UnreadUtil.updateUnreadCount(conversation.getConversationId(),0);
EventCenter.getInstance().onUnreadCountUpdate.send(this, null, null);
conversation.read();
//加入会话
conversation.join(new AVIMConversationCallback() {
@Override
public void done(AVIMException e) {
if (e == null) {
//加入成功
conversation.addMembers(Arrays.asList(chatId), new AVIMConversationCallback() {
@Override
public void done(AVIMException e) {
if (e == null) {
//添加对话成员成功
}
}
});
}
}
});
appMessage = AppMessage.getDefaultMessageTools(conversation);
loadConversation();
}
}
});
这是后台api创建的方法
/**
* 發送消息
* @param msgType
* @param from
* @param toId
* @param title
* @param msg
* @param extMap
* @throws Exception
*/
public void sendMsgAndFCM(String msgType,final String from,final String toId,final String title,final String msg,final Map extMap) throws Exception{
AVHttpClient httpClient = new AVHttpClient();
OkHttpClient okhttpClient = new OkHttpClient();
JSONObject jsonObject = new JSONObject();
//判斷是否存在該會話,是則直接返回會話id
String getConversation="https://"+lcId.substring(0, 8)+".api.lncld.net/1.1/cloudQuery?cql=select * from _Conversation where uniqueId='"+from+"_"+toId+"' and convid='"+from+"_"+toId+"'";
Map map = new HashMap();
map.put("X-LC-Id", lcId);
map.put("X-LC-Key", lcMasterKey);
Headers headers = Headers.of(map);
Request request = getRequest(getConversation,headers, jsonObject,false);
Response response = okhttpClient.newCall(request).execute();
String result = getResponseContent(response.body().byteStream());
JSONObject resultJson = null;
if(result.contains("objectId")){
resultJson = new JSONObject(result).getJSONArray("results").getJSONObject(0);
}else{
//不存在,直接新建會話
jsonObject = new JSONObject();
jsonObject.put("name", "小摩通知");
List list = new ArrayList();
list.add(from);
list.add(toId);
//list.add(toId);
jsonObject.put("m", list);
jsonObject.put("tr", false);
//jsonObject.put("c", toId);
//下面的加了就不能收到了
//jsonObject.put("sys", true);
jsonObject.put("unique", true);
jsonObject.put("uniqueId", from+"_"+toId);
jsonObject.put("convid", from+"_"+toId);
System.out.println(jsonObject.toString());
map.put("X-LC-Key", lcKey);
map.put("Content-Type","application/json");
headers = Headers.of(map);
request = postRequest(conversationUrl,headers, jsonObject,false);
response = okhttpClient.newCall(request).execute();
result = getResponseContent(response.body().byteStream());
System.out.println("result:"+result);
if(result.contains("objectId")){
resultJson = new JSONObject(result);
System.out.println("6666:"+resultJson);
//to = (String);
}else if(result.contains("\"code\":137")){
//to = toId;
}
}
//發送以上建立的會話消息
jsonObject = new JSONObject();
jsonObject.put("conv_id", resultJson.get("objectId"));
jsonObject.put("no_sync", true);
jsonObject.put("from_peer", from);
List toList = new ArrayList();
toList.add(from);
toList.add(toId);
jsonObject.put("to_peers", toList);
JSONObject messageJsonObject = new JSONObject();
messageJsonObject.put("_lctype", -1);
messageJsonObject.put("_lctext", msg);
messageJsonObject.put("_lcattrs", extMap);
jsonObject.put("message", messageJsonObject);
//IOS离线时使用
messageJsonObject = new JSONObject();
messageJsonObject.put("category", "摩美客系统消息");
messageJsonObject.put("_profile", "dev");
messageJsonObject.put("alert", msg);
messageJsonObject.put("title", title);
messageJsonObject.put("body", msg);
messageJsonObject.put("badge", "Increment");
messageJsonObject.put("sound", "default");
messageJsonObject.put("content-available", 0);
//messageJsonObject.put("custom-key", "由用户添加的自定义属性,custom-key 仅是举例,可随意替换");
messageJsonObject.put("type", from);
messageJsonObject.put("from", from);
messageJsonObject.put("to", toList);
messageJsonObject.put("mutable-content", 1);
messageJsonObject.put("extMap", extMap);
jsonObject.put("push_data", messageJsonObject);
jsonObject.put("transient", false);
System.out.println(jsonObject.toString());
map.put("X-LC-Key", lcMasterKey);
headers = Headers.of(map);
httpClient.execute(postRequest(messageUrl,headers, jsonObject,true), false, new AsyncHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] body) {
// TODO Auto-generated method stub
System.out.println(statusCode);
try {
System.out.println("status1:"+statusCode+" "+new String(body,"utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
// TODO Auto-generated method stub
System.out.println("status2:"+statusCode+" "+"error");
error.printStackTrace();
}
});
sendFcm(from, toId, title,msg,extMap);
}
-
创建时间
18年1月2日
-
最后回复
18年1月3日
-
1
回复
-
1.1K
浏览
-
2
用户