1. 程式人生 > >環信整合自定義傳送類似淘寶商品資訊條目

環信整合自定義傳送類似淘寶商品資訊條目

1.專案背景

出於專案的需要,今日一直研究環信整合的IM 傳送類似淘寶商品連結的item ,小有成就 特在此分享: 電商中的及時聊天一般都要把商品的資訊傳送給賣家,以方便雙方的交流,由於之前借鑑 了網上的一些例子,故個別欄位和主題有點出入:
這裡寫圖片描述

2.具體實現步驟

1.環信的訊息擴充套件,首先我們要定義訊息的擴充套件欄位程式碼如下:

    public class MinPianConstant {
    public static final String MYTEXTTYPE="MYTEXTTYPE";//名片擴充套件
    public static final String ICON="img"
; public static final String GOODSNAME="goodsName"; public static final String PRICE="goodsprice"; public static final String DESC="goodsdesc"; }

2.定義EaseMessageAdapter 常量

private static final int REQUEST_TYPE_RECV_CALL=5;
    private static final int REQUEST_TYPE_SEND_CALL=6;

3.建立傳送的訊息

由於從商品詳情也跳轉到聊天頁面都需要傳送商品item 所以每次建立時都發送訊息給對方,但是有些可能從資訊列表跳轉到聊天頁面這裡給了標誌位以作判別:
對話建立時傳送商品詳情的item
  @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        fragmentArgs = getArguments();
        // check if single chat or group chat
        chatType = fragmentArgs.getInt(EaseConstant.EXTRA
_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE); // userId you are chat with or group id toChatUsername = fragmentArgs.getString(EaseConstant.EXTRA_USER_ID); aNew=fragmentArgs.getString("new"); if (aNew!=null){ sendMIngPianMessage("萬聚鮮城 冰島進口新鮮冷凍海蔘斑魚 去髒去砂開背600-800g/條","http://dynamic-image.yesky.com/740x-/uploadImages/2014/289/01/IGS09651F94M.jpg","不錯"); } super.onActivityCreated(savedInstanceState); } 傳送訊息的方法 protected void sendMIngPianMessage(String name, String imv, String desc) { EMMessage message = EMMessage.createTxtSendMessage(name,toChatUsername); message.setAttribute(MinPianConstant.ICON,imv); message.setAttribute(MinPianConstant.MYTEXTTYPE,"mingPianType"); message.setAttribute(MinPianConstant.GOODSNAME,name); message.setAttribute(MinPianConstant.DESC,desc); EMClient.getInstance().chatManager().sendMessage(message); // sendMessage(message); Log.e("at22","成功"); }

3.訊息傳送後下面就要對訊息進行顯示

接下來這個非常重要,這個是名片訊息的ChatRow,就是載體
對,就是new一個chatRow出來,這個chatRow在easeui裡面是沒有的,所以需要我們自己寫

必須注意,一定要繼承EaseChatRow,不然就調不出onInflatView,onFindViewById,onUpdateView,onSetUpView,onBubbleClick這幾個方法

package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.DemoHelper;
import com.hyphenate.easeui.MinPianConstant;
import com.hyphenate.easeui.R;

/**
 * Created by Administrator on 2017/12/6/006.
 */

public class MingPianChatRow extends EaseChatRow {

    public MingPianChatRow(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    protected void onInflateView() {

        if (DemoHelper.getInstance().isMingPianMessage(message)){
            inflater.inflate(message.direct()==EMMessage.Direct.RECEIVE? R.layout.easy_rec_mingpian:R.layout.easy_send_minpian,this);
        }
    }

    ImageView imv;
    TextView tv;
    @Override
    protected void onFindViewById() {

       imv= (ImageView) findViewById(R.id.imv);
       tv= (TextView) findViewById(R.id.tv);


    }

    @Override
    protected void onUpdateView() {

    }

    @Override
    protected void onSetUpView() {

       EMTextMessageBody textMessageBody= (EMTextMessageBody) message.getBody();
        if (message.direct()==EMMessage.Direct.RECEIVE){
            setMessageSendCallback();
            if (textMessageBody.getMessage()!=null){
                tv.setText(textMessageBody.getMessage()+"");
            }

        }
        if (message.getBooleanAttribute("mingPianType",true)){
            String message = this.message.getStringAttribute(MinPianConstant.GOODSNAME, null);
            String imageurl = this.message.getStringAttribute(MinPianConstant.ICON, null);
            tv.setText(message);
            Glide.with(context).load(imageurl).into(imv);
        }

    }

    @Override
    protected void onBubbleClick() {
        Toast.makeText(context,"跳轉頁面",Toast.LENGTH_SHORT).show();
    }
}

佈局檔案 傳送和接受佈局檔案一樣這裡不在說明


> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingTop="13dp" >

    <TextView
        android:id="@+id/timestamp"
        style="@style/chat_text_date_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin_chat_activity" >

        <ImageView
            android:id="@+id/iv_userhead"
            android:layout_width="@dimen/size_avatar"
            android:layout_height="@dimen/size_avatar"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="@dimen/margin_chat_activity"
            android:background="@drawable/ease_default_avatar"
            android:scaleType="fitXY" />

        <RelativeLayout
            android:id="@+id/bubble"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/margin_chat_activity"
            android:layout_toLeftOf="@id/iv_userhead"
            >
            <TextView
                android:layout_toRightOf="@+id/imv"
                android:layout_alignParentRight="true"
                android:id="@+id/tv"
                android:text="123"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/imv"
                android:layout_width="100dp"
                android:layout_height="100dp" />
            <TextView
                android:textColor="@color/holo_red_light"
                android:gravity="center"
                android:text="商品連結"
                android:layout_below="@+id/imv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </RelativeLayout>

        <!--  <TextView
             android:id="@+id/tv_userid"
             android:visibility="gone"
             style="@style/chat_text_name_style"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginRight="15dp"
             android:layout_toLeftOf="@id/iv_userhead"
             android:text="xxx" /> -->

        <ImageView
            android:id="@+id/msg_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/bubble"
            android:clickable="true"
            android:src="@drawable/ease_msg_state_failed_resend"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_ack"
            style="@style/chat_text_name_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/bubble"
            android:text="@string/text_ack_msg"
            android:textSize="12sp"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/tv_delivered"
            style="@style/chat_text_name_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/bubble"
            android:text="@string/text_delivered_msg"
            android:textSize="12sp"
            android:visibility="invisible" />

        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/bubble"
            android:visibility="visible" />
    </RelativeLayout>

</LinearLayout>

根據不同的型別進行建立:

protected EaseChatRow createChatRow(Context context, EMMessage message, int position) {
        EaseChatRow chatRow = null;
        if(customRowProvider != null && customRowProvider.getCustomChatRow(message, position, this) != null){
            return customRowProvider.getCustomChatRow(message, position, this);
        }
        boolean mingPianMessage = DemoHelper.getInstance().isMingPianMessage(message);
        Log.e("at22",mingPianMessage+"---"+message.getType());
        if (DemoHelper.getInstance().isMingPianMessage(message)){

            return new MingPianChatRow(context,message,position,this);
        }

接下來就是在介面卡中顯示了:

    /**
     * get type of item
     */
    public int getItemViewType(int position) {
        EMMessage message = getItem(position); 
        if (message == null) {
            return -1;
        }

        if(customRowProvider != null && customRowProvider.getCustomChatRowType(message) > 0){
            return customRowProvider.getCustomChatRowType(message) + 13;
        }

        if (message.getType() == EMMessage.Type.TXT) {
            if(message.getBooleanAttribute(EaseConstant.MESSAGE_ATTR_IS_BIG_EXPRESSION, false)){
                return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_EXPRESSION : MESSAGE_TYPE_SENT_EXPRESSION;
            }
             if(DemoHelper.getInstance().isMingPianMessage(message)){
                return message.direct()==EMMessage.Direct.RECEIVE?REQUEST_TYPE_RECV_CALL:REQUEST_TYPE_SEND_CALL;
            }//這個很重要
        return -1;// invalid
    }

到最後,我們還需要一個步驟,就是去DemoHelper裡面監聽訊息是不是名片擴充套件訊息

public   class DemoHelper {
    private static final DemoHelper ourInstance = new DemoHelper();

   public   static DemoHelper getInstance() {
        return ourInstance;
    }

    private DemoHelper() {

    }

    public boolean isMingPianMessage(EMMessage emMessage){

        String TYPE = emMessage.getStringAttribute(MinPianConstant.MYTEXTTYPE, null);
        Log.e("at22",TYPE+"");
        if (TYPE==null){
            return false;
        }
        if (TYPE.equals("mingPianType")){
            return true;
        }

        return false;
    }

這樣一個完整的商品擴充套件資訊就寫完了