當前位置:
首頁 > 最新 > 聊天那些事-網易雲信

聊天那些事-網易雲信

最近項目中要用到聊天功能,由於之前一直沒有接觸過通訊方面相關,因此本次文章主要是記錄下在使用過程中的一些步驟和相關注意事件,僅供大家參考!

我們的聊天通訊採用了網易的網易雲信及時通訊聊天,下面以此為例:

一、集成注意事項

根據官方提供的操作手冊接入sdk

2、在使用過程中首先需要在application類中註冊,當然你可以根據nim_demo的源碼進行操作(部分代碼有所刪減)

private void initUIKit() {

// 初始化

NimUIKit.init(this, buildUIKitOptions());

SessionHelper.init();

NimUIKit.setOnlineStateContentProvider(new DemoOnlineStateContentProvider());

}

主要通過

SessionHelper.init();

將UIKit自定義消息界面用法展示類在application中註冊,所以我們只需要註冊好後就可以使用

如果發現如下錯誤,請檢查application類中initUiKit()方法是否和上述內容一致

3、SessionHelper

下面說下SessionHelper這個類,主要決定你的界面能不能顯示出來,如果界面寫好的提前下不能展示出來,那麼就需要注意如下幾點:

SessionHelper中init()方法,是否註冊了消息解析器,是否註冊了擴展消息類型

public static voidinit() {

// 註冊自定義消息附件解析器

NIMClient.getService(MsgService.class).registerCustomAttachmentParser(newCustomAttachParser());

// 註冊各種擴展消息類型的顯示ViewHolder

registerViewHolders();

NimUIKit.setCommonP2PSessionCustomization(getP2pCustomization());

NimUIKit.setRecentCustomization(getRecentCustomization());

}

private static voidregisterViewHolders() {

NimUIKit.registerTipMsgViewHolder(MsgViewHolderTip.class);

NimUIKit.registerMsgItemViewHolder(TipBorrowAttachment.class,MsgViewHolderTipBorrow.class);

}

如果說上面的都註冊過了還是無法顯示,那麼下一步就看是否在自定義組件中添加解析改消息類型的方法

以TipBorrowAttachment類為例:

public classTipBorrowAttachmentextendsCustomAttachment {

private byteflag;

publicTipBorrowAttachment() {

super(CustomAttachmentType.TIPBorrow);

}

publicTipBorrowAttachment(byteflag) {

this();

this.flag= flag;

}

@Override

protectedJSONObjectpackData() {

JSONObject data =newJSONObject();

data.put("flag",flag);

returndata;

}

@Override

protected voidparseData(JSONObject data) {

flag= data.getByte("flag");

}

public bytegetFlag() {

returnflag;

}

publicStringgetContent() {

//首先拿到該聊天人的sessionId

String StrReturnValue ="";

String otherSessionId = (String) SharedInfo.getInstance().getValue("sessionId","");

String name = (String) SharedInfo.getInstance().getValue("chatId","");

//出借人狀態才顯示是否可查看報告

OauthTokenMo mo = SharedInfo.getInstance().getEntity(OauthTokenMo.class);

if(mo !=null) {

if(!TextUtil.isEmpty(mo.getImAccid())) {

//出借人狀態情況下,獲取當前用戶(出借人)的accid得到該擴展欄位來和當前聊天用戶信息進行比對

NimUserInfo nimUserInfo = NIMClient.getService(UserService.class).getUserInfo(mo.getImAccid());

if(mo.getUserType() !=null) {

if(ConstantValue.USER_TYPE_LOAN.equals(mo.getUserType())) {

if(nimUserInfo !=null) {

Map userMap = nimUserInfo.getExtensionMap();

ArrayList userValue = (ArrayList) userMap.get("crm_borrower_report_record");

SharedInfo.getInstance().saveValue("crm_borrower_report_record",userValue.toString());

if(userValue.contains(otherSessionId)) {

StrReturnValue ="("+ name +"信用報告)";

}else{

StrReturnValue ="("+ name.substring(,1) +"**信用報告)";

}

}else{

StrReturnValue ="("+ name.substring(,1) +"**信用報告)";

}

}

}else{

StrReturnValue ="("+ name +"信用報告)";

}

}

}else{

StrReturnValue ="("+ name +"信用報告)";

}

returnStrReturnValue;

}

}

在註冊自定義消息組件時,需要有一個Attachment來進行解析,其中解析的方法就是

@Override

protectedJSONObjectpackData() {

JSONObject data =newJSONObject();

data.put("flag",flag);

returndata;

}

packData()方法就是為了將註冊的消息組件解析出來。

以上就是集成過程中的注意事項

二、聊天相關

部分代碼類提示:

會話聊天可參考:

P2PMessageActivity/MessageFragment/InputPanel

消息列表可參考:

RecentContactsFragment/RecentContactAdapter/RecentViewHolder

會話聊天列表內容展示

MessageListPanelEx

用戶資料操作相關介面

UserService

雲信消息服務介面

MsgService

1、登錄

loginRequest= NimUIKit.login(newLoginInfo(account,token), newRequestCallback() {

@Override

public voidonSuccess(LoginInfo param) {

onLoginDone();

DemoCache.setAccount(account);

saveLoginInfo(account,token);

initNotificationConfig();

}

onSuccess中方法可參考源碼

2、退出

NIMClient.getService(AuthService.class).logout();

3、發送消息

發送伺服器消息(雙方都能接收到)

NIMClient.getService(MsgService.class).sendMessage(IMMessage msg, booleanresend);

發送本地消息(單方面收到)

NIMClient.getService(MsgService.class).saveMessageToLocalEx(msg, true,System.currentTimeMillis());

NIMClient.getService(MsgService.class).saveMessageToLocal(msg, true);

自定義消息類型發送,通過attachment傳遞

TipBorrowAttachment attachment =newTipBorrowAttachment((byte));

IMMessage msg = MessageBuilder.createCustomMessage(sessionId,sessionType,attachment.getContent(),attachment);

NIMClient.getService(MsgService.class).saveMessageToLocalEx(msg, true,System.currentTimeMillis())

4、獲取用戶信息中擴展欄位

NimUserInfo consumeUserInfo = NIMClient.getService(UserService.class).getUserInfo(sessionId);

5、發送消息時添加判斷邏輯

InputPanel——>onTextMessageSendButtonPressed

// 發送文本消息

private void onTextMessageSendButtonPressed() {

String text = messageEditText.getText().toString().trim();

IMMessage textMessage = createTextMessage(replaceSpecStr(text));

if (container.proxy.sendMessage(textMessage)) {

restoreText(true);

}

}

到此為止,網易雲信聊天中需要注意的相關事項就已經結束了,不過最後有一個問題,Android、IOS、H5三方單方面使用自定義消息顯示沒問題,但是不能跨平台,希望知道的小夥伴留個言。


喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 全球大搜羅 的精彩文章:

TAG:全球大搜羅 |