天天看點

環信擴充消息:App内分享資訊進群

最近,公司項目內建了環信即時通訊,提出一個需求:分享app内文章資訊進環信群。先上個效果圖(UI我随便放的,大家湊活看** — **):

環信擴充消息:App内分享資訊進群

先放上參考文章連結:https://www.jianshu.com/p/0692396f2fbe,在這裡先感謝作者!在這篇文章裡作者是通過在EaseChatExtendMenu 擴充,注冊item的方式實作的發送名片功能,具體的大家可以看這篇文章。

我這裡其實跟參考文章的原理是一樣的,也是通過擴充消息的方式,自定義一個消息類型,通過發送text文本消息,自定義了消息UI,下面是實作步驟:

一、首先自定義擴充字段:

public class NewsConstant {
    public static final String newsExtType = "newsExtType";//資訊資訊擴充 
     public static final String newsId = "newsId";//資訊資訊ID  
    public static final String newsTitle = "newsTitle";//資訊标題  
     public static final String newsDesc = "newsDesc";//資訊資訊小文字描述  
     public static final String newsImgUrl = "newsImgUrl";//資訊圖檔
}      

二、在ChatFragment中定義發送資訊消息和接收資訊消息的辨別符,去CustomChatRowProvider内部類中設定接收和發送資訊消息:

1、在ChatFragment中定義發送資訊消息和接收資訊消息的辨別符:

private static final int MESSAGE_TYPE_RECV_NEWS = 5;
private static final int MESSAGE_TYPE_SEND_NEWS = 6;      

2、去CustomChatRowProvider内部類中設定接收和發送資訊消息:

private final class CustomChatRowProvider implements EaseCustomChatRowProvider {
    @Override
    public int getCustomChatRowTypeCount() {
        //here the number is the message type in EMMessage::Type
        //which is used to count the number of different chat row
        return 13;
    }

    @Override
    public int getCustomChatRowType(EMMessage message) {
        if (message.getType() == EMMessage.Type.TXT) {
            //voice call
            if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false)) {
                return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VOICE_CALL : MESSAGE_TYPE_SENT_VOICE_CALL;
            } else if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, false)) {
                //video call
                return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VIDEO_CALL : MESSAGE_TYPE_SENT_VIDEO_CALL;
            }
            //messagee recall
            else if (message.getBooleanAttribute(Constant.MESSAGE_TYPE_RECALL, false)) {
                return MESSAGE_TYPE_RECALL;
            } else try {
                if ("1".equals(message.getStringAttribute(NewsConstant.newsExtType))) {
                    return message.direct() == EMMessage.Direct.RECEIVE ? ChatFragment.MESSAGE_TYPE_RECV_NEWS : ChatFragment.MESSAGE_TYPE_SEND_NEWS;
                }
            } catch (HyphenateException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    @Override
    public EaseChatRowPresenter getCustomChatRow(EMMessage message, int position, BaseAdapter adapter) {
        if (message.getType() == EMMessage.Type.TXT) {
            if (message.getBooleanAttribute(Constant.MESSAGE_TYPE_RECALL, false)) {
                EaseChatRowPresenter presenter = new EaseChatRecallPresenter();
                return presenter;
            } else try {
                if ("1".equals(message.getStringAttribute(NewsConstant.newsExtType))) {
                    //發送資訊資訊卡片  
                    return new EaseChatNewsCallPresenter();
                }
            } catch (HyphenateException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}      

注意:在getCustomChatRowTypeCount()方法中,傳回值記得加2,代表發送和接收資訊消息

            如果想設定消息的點選事件,可以在onMessageBubbleClick()方法中實作:

@Override
public boolean onMessageBubbleClick(EMMessage message) {
    try {
        //點選資訊消息事件
        if ("1".equals(message.getStringAttribute(NewsConstant.newsExtType))) {
            Intent intent = new Intent(getActivity(), ArticleDetailsActivity.class);
            intent.putExtra("newsId", Integer.parseInt(message.getStringAttribute(NewsConstant.newsId)));
            startActivity(intent);
            return true;
        }
    } catch (HyphenateException e) {
        e.printStackTrace();
    }
    return false;
}      

三、自定義EaseChatNewsCallPresenter:

public class EaseChatNewsCallPresenter extends EaseChatRowPresenter {
    
    @Override
    protected EaseChatRow onCreateChatRow(Context cxt, EMMessage message, int position, BaseAdapter adapter) {
        return new NewsChatRow(cxt, message, position, adapter);
    }
}      

四、非常重要!!!自定義NewsChatRow,也就是擴充消息的載體:

public class NewsChatRow extends EaseChatRow {

    private TextView title, desc;
    private ImageView img;

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

    /* 
    填充layout 
     */
    @Override
    protected void onInflateView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_news_call : R.layout.ease_row_send_news_call, this);
    }

    /* 
    查找chatrow中的控件 
     */
    @Override
    protected void onFindViewById() {
        title = (TextView) findViewById(R.id.news_card_title);
        desc = (TextView) findViewById(R.id.news_card_desc);
        img = (ImageView) findViewById(R.id.news_card_img);
    }

    /* 
    消息狀态改變,重新整理listView 
     */
    @Override
    protected void onViewUpdate(EMMessage msg) {
        switch (msg.status()) {
            case CREATE:
                onMessageCreate();
                break;
            case SUCCESS:
                onMessageSuccess();
                break;
            case FAIL:
                onMessageError();
                break;
            case INPROGRESS:
                onMessageInProgress();
                break;
        }
//        adapter.notifyDataSetChanged();
    }
    
    @Override
    protected void onSetUpView() {
        EMTextMessageBody textMessageBody = (EMTextMessageBody) message.getBody();
        if (message.direct()== EMMessage.Direct.RECEIVE){
            LogUtils.e(textMessageBody.getMessage());
        }
//        message.getStringAttribute(NewsConstant.newsExtType, null);
        String titleStr = message.getStringAttribute(NewsConstant.newsTitle, null);
        String descStr = message.getStringAttribute(NewsConstant.newsDesc, null);
        String imgurlStr = message.getStringAttribute(NewsConstant.newsImgUrl, null);
        if (!TextUtils.isEmpty(titleStr))
            title.setText(titleStr);
        if (!TextUtils.isEmpty(descStr))
            desc.setText(descStr);
        Glide.with(context)
                .load(imgurlStr)
                .error(R.drawable.icon_logo)
                .placeholder(R.drawable.loading_img)
                .crossFade()
                .into(img);
    }

    private void onMessageCreate() {
        progressBar.setVisibility(View.VISIBLE);
        statusView.setVisibility(View.GONE);
    }

    private void onMessageSuccess() {
        progressBar.setVisibility(View.GONE);
        statusView.setVisibility(View.GONE);
    }

    private void onMessageError() {
        progressBar.setVisibility(View.GONE);
        statusView.setVisibility(View.VISIBLE);
    }

    private void onMessageInProgress() {
        progressBar.setVisibility(View.VISIBLE);
        statusView.setVisibility(View.GONE);
    }
}      

到這裡用擴充消息實作發送資訊進群的功能基本就實作了,下面就是分享資訊消息進群了:

發送資訊消息事件:

EMMessage message = EMMessage.createTxtSendMessage("[分享]" + mainActivity.getData("newsTitle")
        , username);
message.setAttribute(NewsConstant.newsExtType, "1");//設定擴充字段  
message.setAttribute(NewsConstant.newsTitle, mainActivity.getData("newsTitle"));//将title設定到擴充字段  
message.setAttribute(NewsConstant.newsImgUrl, mainActivity.getData("newsImg"));//将imgUrl設定到擴充字段  
message.setAttribute(NewsConstant.newsId, mainActivity.getData("newsId"));//将newsId設定到擴充字段  
message.setAttribute(NewsConstant.newsDesc, mainActivity.getData("newsDesc"));//将desc設定到擴充字段
Intent intent = new Intent(getActivity(), ChatActivity.class);
if (conversation.isGroup()) {
    if (conversation.getType() == EMConversation.EMConversationType.ChatRoom) {
        // it's group chat
        message.setChatType(EMMessage.ChatType.ChatRoom);
        intent.putExtra(Constant.EXTRA_CHAT_TYPE, Constant.CHATTYPE_CHATROOM);
    } else {
        message.setChatType(EMMessage.ChatType.GroupChat);//注意:這行代碼一定不要忘記,環信預設消息的類型是單聊,這行代碼是改成群聊消息類型,否則會導緻群聊消息接收不到
        intent.putExtra(Constant.EXTRA_CHAT_TYPE, Constant.CHATTYPE_GROUP);
    }
}
// it's single chat
intent.putExtra(Constant.EXTRA_USER_ID, username);
EMClient.getInstance().chatManager().sendMessage(message);
startActivity(intent);      

注意:自定義擴充消息的字段名一定要跟IOS小夥伴的一緻!!!

到這裡擴充消息分享就完成了,有需要的童鞋可以看看

環信擴充消息:App内分享資訊進群