admin管理员组文章数量:1621113
之前做的简单的聊天功能,今天让新加黑名单和免打扰功能,用的极光的im。
发现极光im的消息发送回调只有 code状态码 和 String的提示语,如下:
BasicCallback jmCallBack = new BasicCallback() { //统一的回调管理类
@Override
public void gotResult(int i, String s) {
if (i == 0) { //success
} else if (i == 803009) { //发送禁词
UI.showToast(ChatSingBetterGoodActivity.this, "包含敏感词");
} else if (i == 803008) { //被对方拉黑了
}
}
};
没有回调msgId,那么问题就来了,如果我一下子发送很多条消息。我怎么知道哪条消息发送失败了,哪条消息发送被拉黑拒收了呢?
又看了一遍人家demo的代码,总算是把思路理出来了。 在adapter中添加item数据和判断消息的状态,如果是going就另起回调监听这一行的数据刷新,如下,重点是红色这行:
添加item数据的方法:
public void addMsgToList(Message msg) {
mData.add(msg);
}
onBindViewHolder中判断消息的状态:
if (m.getStatus() == MessageStatus.send_success) {
holder2.my_send_error.setVisibility(View.GONE);
} else if (m.getStatus() == MessageStatus.send_going) {
holder2.my_send_error.setVisibility(View.GONE);
chatItemController.handleMsg(m, holder2, position);
}else if (m.getStatus() == MessageStatus.send_fail) {
holder2.my_send_error.setVisibility(View.VISIBLE);
}
chatItemController中的handleMsg方法
public void handleTextMsg(final Message msg, final ChatSingBetterGoodAdapter.MyViewHolder holder, int position) {
// 检查发送状态,发送方有重发机制
if (msg.getDirect() == MessageDirect.send) {
switch (msg.getStatus()) {
case created:
holder.my_send_error_img.setVisibility(View.GONE);
holder.my_send_error_custom.setVisibility(View.GONE);
holder.my_send_error.setVisibility(View.GONE);
break;
case send_success:
holder.my_send_error_img.setVisibility(View.GONE);
holder.my_send_error_custom.setVisibility(View.GONE);
holder.my_send_error.setVisibility(View.GONE);
break;
case send_fail:
if(msg.getContentType() == ContentType.text){
holder.my_send_error_img.setVisibility(View.GONE);
holder.my_send_error_custom.setVisibility(View.GONE);
holder.my_send_error.setVisibility(View.VISIBLE);
}else if(msg.getContentType() == ContentType.image){
holder.my_send_error_img.setVisibility(View.VISIBLE);
holder.my_send_error_custom.setVisibility(View.GONE);
holder.my_send_error.setVisibility(View.GONE);
}else if(msg.getContentType() == ContentType.custom){
holder.my_send_error_img.setVisibility(View.GONE);
holder.my_send_error_custom.setVisibility(View.VISIBLE);
holder.my_send_error.setVisibility(View.GONE);
}
break;
case send_going: //发送中的消息 持有holder对象可以随时刷新
sendingholder(holder, msg);
break;
default:
}
}
}
private void sendingholder(final ChatSingBetterGoodAdapter.MyViewHolder holder, final Message msg) {
//消息正在发送,重新注册一个监听消息发送完成的Callback
msg.setOnSendCompleteCallback(new BasicCallback() {
@Override
public void gotResult(final int status, final String desc) {
Log.e("803008",status+"");
if (status == 803008) {//被对方拉黑了
holder.tv_chat_my_is_in_other_black_list.setVisibility(View.VISIBLE);
if(msg.getContentType() == ContentType.text){
holder.my_send_error.setVisibility(View.VISIBLE);
}else if(msg.getContentType() == ContentType.image){
holder.my_send_error_img.setVisibility(View.VISIBLE);
}else if(msg.getContentType() == ContentType.custom){
holder.my_send_error_custom.setVisibility(View.VISIBLE);
}
//显示出拉黑的提示 但是还需要把list滚动到最底部
Intent i = new Intent("showBlackTipToScrollBottom");
mContext.sendBroadcast(i);
} else if (status == 803005) {
holder.tv_chat_my_is_in_other_black_list.setVisibility(View.GONE);
if(msg.getContentType() == ContentType.text){
holder.my_send_error.setVisibility(View.VISIBLE);
}else if(msg.getContentType() == ContentType.image){
holder.my_send_error_img.setVisibility(View.VISIBLE);
}else if(msg.getContentType() == ContentType.custom){
holder.my_send_error_custom.setVisibility(View.VISIBLE);
}
}else if (status == 803009) { //发送禁词了
UI.showToast(mContext,"包含敏感词");
holder.tv_chat_my_is_in_other_black_list.setVisibility(View.GONE);
if(msg.getContentType() == ContentType.text){
holder.my_send_error.setVisibility(View.VISIBLE);
}else if(msg.getContentType() == ContentType.image){
holder.my_send_error_img.setVisibility(View.VISIBLE);
}else if(msg.getContentType() == ContentType.custom){
holder.my_send_error_custom.setVisibility(View.VISIBLE);
}
} else if (status != 0) {
holder.tv_chat_my_is_in_other_black_list.setVisibility(View.GONE);
if(msg.getContentType() == ContentType.text){
holder.my_send_error.setVisibility(View.VISIBLE);
}else if(msg.getContentType() == ContentType.image){
holder.my_send_error_img.setVisibility(View.VISIBLE);
}else if(msg.getContentType() == ContentType.custom){
holder.my_send_error_custom.setVisibility(View.VISIBLE);
}
}
}
});
}
大概思路就是这样了。
版权声明:本文标题:极光im发送Message被拉黑和消息发送失败的处理办法 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1725263339a1015432.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论