網易雲信自定義消息(類似微信分享界面)


 
首先,我們先定義一個自定義消息的類型
public interface CustomAttachmentType {
// 多端統一
int Guess = 1;
int SnapChat = 2;
int Sticker = 3;
int RTS = 4;
int SHARE = 5;//自定義
}
第二步,我們先定義一個自定義消息附件的基類,負責解析你的自定義消息的公用字段,比如類型等

注意: 實現 MsgAttachment 接口的成員都要實現 Serializable。(這個類demo里面有的,猜拳用的)

public abstract class CustomAttachment implements MsgAttachment {
    protected int type;
    CustomAttachment(int type) {
        this.type = type;
    }
    public void fromJson(JSONObject data) {
        if (data != null) {
            parseData(data);
        }
    }
    @Override
    public String toJson(boolean send) {

        return CustomAttachParser.packData(type, packData());
    }
    public int getType() {
        return type;
    }
    protected abstract void parseData(JSONObject data);
    protected abstract JSONObject packData();
}

第三步,繼承這個基類,實現“分享”的附件類型。注意,成員變量都要實現 Serializable

 

public class fghGuessAttachment extends CustomAttachment {


private String url = "http://img0.imgtn.bdimg.com/it/u=1737766921,271555379&fm=21&gp=0.jpg";//這個圖片沒使用,在布局里面放張默認圖片
private String content = "內容xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private String title="標題xxxx";

protected int type;


public fghGuessAttachment() {
super(CustomAttachmentType.SHARE);
}
public fghGuessAttachment(String str) {
this();
}

@Override
protected void parseData(JSONObject data) {
title=data.getString("title");
content=data.getString("content");
url=data.getString("url");
}

@Override
public JSONObject packData() {
JSONObject data = new JSONObject();
data.put("title",title);
data.put("content",content);
data.put("url",url);
return data;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
第四步,實現自定義消息的附件解析器。
public class CustomAttachParser implements MsgAttachmentParser {//類里面
 
case CustomAttachmentType.SHARE:
attachment = new fghGuessAttachment();
break;
 
第五步,將自定義消息展示UI上

viewholder包名下
/**
* UIKit自定義消息界面用法展示類
*/
public class fghSessionHelper extends MsgViewHolderBase{


private fghGuessAttachment attachment;
private ImageView image;
private TextView tvTitle;
private TextView tvContent;

@Override
protected int getContentResId() {
return R.layout.aaaaaaaa;
}

@Override
protected void inflateContentView() {
image = (ImageView) findViewById(R.id.image);
tvTitle = (TextView) findViewById(R.id.tv_title);
tvContent = (TextView) findViewById(R.id.tv_content);

}

@Override
protected void bindContentView() {
attachment = (fghGuessAttachment)message.getAttachment();
tvTitle.setText(attachment.getTitle());
tvContent.setText(attachment.getContent());
}
//若是要自己修改氣泡背景
// 當是發送出去的消息時,內容區域背景的drawable id
@Override
protected int rightBackground() {
return com.netease.nim.uikit.R.drawable.nim_message_item_right_selector2;
}

}
第六步,發送自定義消息
public class fghGuessAction extends BaseAction {

public fghGuessAction() {
super(R.drawable.message_plus_guess_selector, R.string.input_panel_share);
}

@Override
public void onClick() {
fghGuessAttachment attachment = new fghGuessAttachment();
IMMessage message;
if (getContainer() != null && getContainer().sessionType == SessionTypeEnum.ChatRoom) {
message = ChatRoomMessageBuilder.createChatRoomCustomMessage(getAccount(), attachment);
} else {
message = MessageBuilder.createCustomMessage(getAccount(), getSessionType(), attachment);
}

sendMessage(message);
}
}
 
        

 

第七步,將該附件解析器注冊到 SDK 中。為了保證生成歷史消息時能夠正確解析自定義附件,注冊一般應放在 Application 的 onCreate 中完成
NIMClient.getService(MsgService.class).registerCustomAttachmentParser(new CustomAttachParser()); // 監聽的注冊,必須在主進程中。


第八步注冊擴展消息類型的顯示ViewHolder,由於這里使用我們UIKIT,所以也需要注冊到Application的onCreate中
public class SessionHelper {//這個類里面
 
//fgh
NimUIKit.registerMsgItemViewHolder(fghGuessAttachment.class,fghSessionHelper.class);


第九步,在加號里面增加一個選項 ,Demo是在SessionHelper.java里面,定制的單聊界面。
/**
* UIKit自定義消息界面用法展示類
*/
public class SessionHelper {

 
        

// 定制加號點開后可以包含的操作, 默認已經有圖片,視頻等消息了
ArrayList<BaseAction> actions = new ArrayList<>();
actions.add(new fghGuessAction());
 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM