自定义消息需要继承IMChattingPageOperateion
/**
* Created by great小海海 on 2017/8/23.
* 自定义消息,目前有两种商品和订单
*/
public class ChattingOperationCustom extends IMChattingPageOperateion{
private DisplayImageOptions options;
//自定义消息view的种类数
private final int typeCount = 2;
/** 自定义viewType,viewType的值必须从0开始,然后依次+1递增,且viewType的个数必须等于typeCount,切记切记!!!***/
//
private final int type_0 = 0;
//
private final int type_1 = 1;
public static String goodsJson;
public ChattingOperationCustom(Pointcut pointcut) {
super(pointcut);
}
public static void getMessage(String goods_name,String goods_id,String goods_price,String goods_image,String goods_url){
JSONObject object = new JSONObject();
try {
// object.put("customizeMessageType");
object.put("kbn","1");
object.put("goods_name",goods_name);
object.put("goods_price", goods_price+"Ks");
object.put("goods_id",goods_id);
object.put("goods_image",goods_image);
object.put("goods_url",goods_url);
} catch (JSONException e) {
}
goodsJson=object.toString();
}
/**
* 自定义消息view的种类数
* @return 自定义消息view的种类数
*/
@Override
public int getCustomViewTypeCount() {
return typeCount;
}
/**
* 自定义消息view的类型,开发者可以根据自己的需求定制多种自定义消息view,这里需要根据消息返回view的类型
* @param message 需要自定义显示的消息
* @return 自定义消息view的类型
*/
@Override
public int getCustomViewType(YWMessage message) {
//根据消息的类型是单聊,群聊还是其他的类型,本次只涉及单聊
ImCustomBean imCustomBean= null;
if (message.getSubType() == YWMessage.SUB_MSG_TYPE.IM_P2P_CUS){
String msgType = null;
try {
String content = message.getMessageBody().getContent();
Gson gson = new Gson();
imCustomBean = gson.fromJson(content, ImCustomBean.class);
} catch (Exception e) {
}
Log.e("IM", "getCustomViewTypeJson: "+message.getMessageBody().getContent() );
Log.e("IM", "getCustomViewType: "+imCustomBean.getKbn());
if (imCustomBean.getKbn().toString().equals("0"))
{
return type_1;
}else {
return type_0;
}
}
return super.getCustomViewType(message);
}
/**
* 根据viewType获取自定义view
* @param fragment 聊天窗口fragment
* @param message 当前需要自定义view的消息
* @param convertView 自定义view
* @param viewType 自定义view的类型
* @param headLoadHelper 头像加载管理器,用户可以调用该对象的方法加载头像
* @return 自定义view
*/
@Override
public View getCustomView(Fragment fragment, YWMessage message, View convertView, int viewType, YWContactHeadLoadHelper headLoadHelper) {
YWLog.i("YunWang", "getCustomView, type = " + viewType);
if (viewType == type_0) { //商品详情
ViewHolder0 holder = null;
if (convertView == null) {
//商品详情布局
holder = new ViewHolder0();
convertView = View.inflate(fragment.getActivity(), R.layout.de_item_shop_message_mix, null);
holder.itemTitleMix = (TextView) convertView.findViewById(R.id.item_title_mix);
holder.itemSubtitleMix = (TextView) convertView.findViewById(R.id.item_subtitle_mix);
holder.itemImage = ((ImageView) convertView.findViewById(R.id.item_image));
holder.goodsDetail = ((LinearLayout) convertView.findViewById(R.id.goods_detail));
convertView.setTag(holder);
YWLog.i("YunWang", "getCustomView, convertView == null");
} else {
holder = (ViewHolder0) convertView.getTag();
YWLog.i("YunWang", "getCustomView, convertView != null");
}
String content = message.getMessageBody().getContent();
Gson gson = new Gson();
ImCustomBean imCustomBean = gson.fromJson(content, ImCustomBean.class);
holder.itemTitleMix.setText(imCustomBean.getGoods_name());
holder.itemSubtitleMix.setText(imCustomBean.getGoods_price());
setImageView(imCustomBean.getGoods_image(),holder.itemImage);
return convertView;
} else if (viewType == type_1) {
//订单详情布局
ViewHolder1 holder = null;
if (convertView == null){
holder = new ViewHolder1();
convertView = View.inflate(fragment.getActivity(), R.layout.de_item_shop_message, null);
holder.itemTitle = ((TextView) convertView.findViewById(R.id.item_title));
holder.itemSubtitle = ((TextView) convertView.findViewById(R.id.item_subtitle));
holder.orderFlag = (RelativeLayout) convertView.findViewById(R.id.order_flag);
convertView.setTag(holder);
}else {
holder = ((ViewHolder1) convertView.getTag());
}
String content = message.getMessageBody().getContent();
Gson gson = new Gson();
ImCustomBean imCustomBean = gson.fromJson(content, ImCustomBean.class);
holder.itemTitle.setText(imCustomBean.getTitle());
holder.itemSubtitle.setText(imCustomBean.getSubtitle());
return convertView;
}
return super.getCustomView(fragment, message, convertView, viewType, headLoadHelper);
}
public void setImageView(String url, ImageView imageView){
options = new DisplayImageOptions.Builder().cacheInMemory(true) // 启用内存缓存
.cacheOnDisk(true) // 启用外存缓存
.considerExifParams(true) // 启用EXIF和JPEG图像格式
.displayer(new SimpleBitmapDisplayer()) // 设置显示风格这里是圆角矩形
.showImageOnLoading(R.drawable.mrpic)
.showImageForEmptyUri(R.drawable.mrpic)
.showImageOnFail(R.drawable.mrpic)
.build();
ImageLoader.getInstance().displayImage(url, imageView, options);
}
//商品详情
public class ViewHolder0 {
LinearLayout goodsDetail;
TextView itemTitleMix,itemSubtitleMix;
ImageView itemImage;
}
//订单
public class ViewHolder1 {
TextView itemTitle;
TextView itemSubtitle;
RelativeLayout orderFlag;
}
@Override
public boolean onMessageClick(Fragment fragment, YWMessage message) {
//自定义消息的点击事件(单聊和群聊的)
if (message.getSubType() == YWMessage.SUB_MSG_TYPE.IM_P2P_CUS || message.getSubType() == YWMessage.SUB_MSG_TYPE.IM_TRIBE_CUS){
try {
String content = message.getMessageBody().getContent();
Log.e("onclick", "onMessageClick: "+content );
Gson gson = new Gson();
ImCustomBean imCustomBean = gson.fromJson(content, ImCustomBean.class);
if (!TextUtils.isEmpty(imCustomBean.getUrl())){
String url = imCustomBean.getUrl();
skipToActivity(fragment, url);
}
if (!TextUtils.isEmpty(imCustomBean.getGoods_url())){
String url = imCustomBean.getGoods_url();
skipToActivity(fragment, url);
}
} catch (Exception e) {
}
return true;
}
return super.onMessageClick(fragment, message);
}
private void skipToActivity(Fragment fragment, String url) {
Intent intent = new Intent(fragment.getActivity(), GoodsInfoActivity.class);
intent.putExtra("url",url);
fragment.getActivity().startActivity(intent);
}
/**
/**
* 当打开聊天窗口时,自动发送该消息给对方
* @param fragment 聊天窗口fragment
* @param conversation 当前会话
* @param isConversationFirstCreated 是否是首次创建会话
* @return 自动发送的消息(注意,内容empty则不自动发送)
*/
@Override
public YWMessage ywMessageToSendWhenOpenChatting(Fragment fragment, YWConversation conversation, boolean isConversationFirstCreated) {
if (isConversationFirstCreated && !TextUtils.isEmpty(goodsJson)){
YWCustomMessageBody messageBody = new YWCustomMessageBody();
messageBody.setContent(goodsJson); // 用户要发送的自定义消息,SDK不关心具体的格式,比如用户可以发送JSON格式
messageBody.setSummary("goods detail"); // 可以理解为消息的标题,用于显示会话列表和消息通知栏
YWMessage message = YWMessageChannel.createCustomMessage(messageBody);
return message;
}else {
return null;
}
}
}
针对需要首次打开聊天窗口自动发送消息:
ChattingOperationCustom.getMessage(mGoodsTitle,mGoodsID,mGoodsPrice,mGoodsImage,mGoodsUrl);
Intent intent = LoginHelper.getsInstance().getIMKit().getChattingActivityIntent(userid);
startActivity(intent);
当然还需要一些逻辑上的判定,根据需求自定