美文网首页Android开发
接入融云IM-自定义中间提示信息

接入融云IM-自定义中间提示信息

作者: 你的益达233 | 来源:发表于2022-05-07 10:42 被阅读0次

一、前言

这样的需求也比较常见,实现起来也比较简单。但往往要控制谁能看见,谁不能看见的。

二、效果图

提示消息.png

三、解决思路

正常的自定义消息应该你们会了。说下如何控制谁能看见问题。如果要通知栏显示,即消息需要有app服务器发送,服务端发送的api中有个setIsIncludeSender方法。不要通知栏的,app服务器发送透传消息给app,app判断后本地添加。

四、关键的提示消息体和模板代码

@MessageTag(value = "customize_tip_message", flag = 3)

public class CustomizeTipsMessage extends MessageContent {

    public static final int DEFAULT_TYPE = 0; //0不进行任何跳转
    public static final int JUMP_BAIDU_MATCH_TYPE = 1; //跳转到百度真人认证
    public static final int JUMP_MY_AWARD = 2; //跳转到我的奖励(积分界面)
    public static final int JUMP_CHAT_SETTING = 3; //跳转到聊天设置
    public static final int JUMP_GIFT_DIALOG = 4; //弹起送礼物弹框
    private String content; //整体文案,传的是html文本,因为有些文本需要高亮显示
    private int showLeftTip; //默认0不显示 ,1为显示,用int,不用布尔型
    // 示例
    /**
     * tvUsedTime.setText(Html.fromHtml("认证状态:<font color='#aaaaaa'>认证审核中</font>你们好"));
     *  如果接口只需返回“认证状态:<font color='#aaaaaa'>认证审核中</font>你们好”
     */
    private int jumpType; //用于判断点击跳转
    private String outContent; //用于外面会话列表的文案,为空时统一用“温馨提示”文案


    public String getContent() {
        return content;
    }

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

    public int getJumpType() {
        return jumpType;
    }

    public void setJumpType(int jumpType) {
        this.jumpType = jumpType;
    }

    public String getOutContent() {
        return outContent;
    }

    public void setOutContent(String outContent) {
        this.outContent = outContent;
    }

    public int getShowLeftTip() {
        return showLeftTip;
    }

    public void setShowLeftTip(int showLeftTip) {
        this.showLeftTip = showLeftTip;
    }

    public CustomizeTipsMessage(String content, int jumpType, int showLeftTip,String outContent) {
        this.content = content;
        this.jumpType = jumpType;
        this.showLeftTip = showLeftTip;
        this.outContent = outContent;
    }

    @Override
    public byte[] encode() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("content",getContent());
            jsonObject.put("jumpType",getJumpType());
            jsonObject.put("showLeftTip",getShowLeftTip());
            jsonObject.put("outContent",getOutContent());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try{
            return jsonObject.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public CustomizeTipsMessage(byte [] data){
        String str = null ;
        try{
            str = new String(data,"UTF-8") ;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try{
            JSONObject json = new JSONObject(str);
            if(json.has("content")){
                content = json.optString("content");
            }
            if(json.has("jumpType")){
                jumpType = json.optInt("jumpType");
            }
            if(json.has("showLeftTip")){
                showLeftTip = json.optInt("showLeftTip");
            }
            if(json.has("outContent")){
                outContent = json.optString("outContent");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public CustomizeTipsMessage(Parcel in){
        this.content = ParcelUtils.readFromParcel(in) ;
        this.jumpType = ParcelUtils.readIntFromParcel(in);
        this.showLeftTip = ParcelUtils.readIntFromParcel(in);
        this.outContent = ParcelUtils.readFromParcel(in) ;
    }

    public static final Creator<CustomizeTipsMessage> CREATOR = new Creator<CustomizeTipsMessage>() {
        @Override
        public CustomizeTipsMessage createFromParcel(Parcel parcel) {
            return new CustomizeTipsMessage(parcel);
        }

        @Override
        public CustomizeTipsMessage[] newArray(int i) {
            return new CustomizeTipsMessage[i];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        ParcelUtils.writeToParcel(parcel,content);
        ParcelUtils.writeToParcel(parcel,jumpType);
        ParcelUtils.writeToParcel(parcel,showLeftTip);
        ParcelUtils.writeToParcel(parcel,outContent);
    }}

CustomizeTipsMessageProvider

class CustomizeTipsMessageProvider : BaseMessageItemProvider<CustomizeTipsMessage>() {
    override fun onCreateMessageContentViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val rootView =
            LayoutInflater.from(parent.context).inflate(R.layout.rc_my_tip_item, parent, false)
        ShapeUtils.setShapeCorner2Color(
            rootView,
            parent.context.resources.getColor(R.color.color_EA),
            dp2px(parent.context, 6).toFloat()
        )
        //parent.setBackgroundColor(parent.context.getColors(R.color.rc_background_main_color))
        return ViewHolder(parent.context, rootView)
    }

    override fun bindMessageContentViewHolder(
        viewHolder: ViewHolder,
        parentHolder: ViewHolder,
        customizeTipsMessage: CustomizeTipsMessage,
        uiMessage: UiMessage,
        i: Int,
        list: List<UiMessage>,
        iViewProviderListener: IViewProviderListener<UiMessage>
    ) {
        //Log.i("bindMessageContentViewHolder1",customizeTipsMessage.getContent());
        //LogUtils.INSTANCE.o("bindMessageContentViewHolder1",customizeTipsMessage);
        (viewHolder.getView<View>(R.id.tv_tip_content) as TextView).text =
            Html.fromHtml(customizeTipsMessage.content)
        viewHolder.setVisible(R.id.tv_tip_tag,customizeTipsMessage.showLeftTip== 1)

    }

    override fun bindViewHolder(
        holder: ViewHolder,
        uiMessage: UiMessage?,
        position: Int,
        list: MutableList<UiMessage>?,
        listener: IViewProviderListener<UiMessage>?
    ) {
        super.bindViewHolder(holder, uiMessage, position, list, listener)
        holder.setBackgroundColor(R.id.rc_content,holder.context.getColors(R.color.rc_background_main_color))
    }

    override fun onItemClick(
        viewHolder: ViewHolder,
        customizeTipsMessage: CustomizeTipsMessage,
        uiMessage: UiMessage,
        i: Int,
        list: List<UiMessage>,
        iViewProviderListener: IViewProviderListener<UiMessage>
    ): Boolean {
        LogUtils.i("onItemClickTip",customizeTipsMessage.jumpType.toString())
        when (customizeTipsMessage.jumpType) {
            CustomizeTipsMessage.JUMP_BAIDU_MATCH_TYPE -> {
                AppRouter.goRealPersonAuth()
                return true
            }
            CustomizeTipsMessage.JUMP_MY_AWARD -> {
                CRouter.go(AppRouter.appProfit)
                return true
            }
            CustomizeTipsMessage.JUMP_CHAT_SETTING -> {
                CRouter.go(AppRouter.appChatSetting)
                return true
            }
            CustomizeTipsMessage.JUMP_GIFT_DIALOG -> {
                try {
                    val toUserId = AES128ECB.Decrypt(uiMessage.targetId).toInt() //解密 后对方的用户ID;
                    CommonViewModel(CommonRepository()).getGiftPrices(viewHolder.context!!,toUserId){ giftPrice, giftNum ->

                        ImUtils.sendGiftMessage(giftPrice.img,giftPrice.name,giftNum,
                            CustomizeGiftsMessage.NORMAL_GIFT_TYPE,uiMessage.targetId)

                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                return true
            }
        }
        return false
    }

    override fun isMessageViewType(messageContent: MessageContent): Boolean {
        return messageContent is CustomizeTipsMessage
    }

    override fun getSummarySpannable(
        context: Context,
        customizeTipsMessage: CustomizeTipsMessage
    ): Spannable {
        if (!customizeTipsMessage.outContent.isNullOrEmpty()){
            return SpannableString(customizeTipsMessage.outContent)
        }
        return SpannableString(context.getString(R.string.warn_msg_tip))
    }

    /**
     * @desc : 配置头像是否显示,居中显示
     * @author : congge on 2022-02-18 18:14
     */
    init {
        mConfig.showPortrait = false
        mConfig.centerInHorizontal = true
}}

有问题的可以私信我,解决融云IM的各种问题。

相关文章

网友评论

    本文标题:接入融云IM-自定义中间提示信息

    本文链接:https://www.haomeiwen.com/subject/nqnturtx.html