本文主要是介绍Android 融云单聊与群聊消息免打扰功能设置与消息删除功能实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一.设置群聊与单聊消息免打扰功能:
1.下面直接进入逻辑代码:
实现监听事件:
/*** 设置会话列表界面操作的监听器。*/RongIM.setConversationListBehaviorListener(new MyConversationListBehaviorListener());
public class MyConversationListBehaviorListener implements RongIM.ConversationListBehaviorListener {}
2.上面的监听有四个方法:
(1).头像点击事件
@Override
public boolean onConversationPortraitClick(Context context, Conversation.ConversationType conversationType, String s) {
return false;
}
(2).头像长按事件
@Override
public boolean onConversationPortraitLongClick(Context context, Conversation.ConversationType conversationType, String s) {
return false;
}
(3).消息点击事件
@Override
public boolean onConversationClick(Context context, View view, UIConversation uiConversation) {
return false;
}
(4).消息长按事件
@Override
public boolean onConversationLongClick(final Context context, View view, final UIConversation uiConversation) {
return false;
}
3.我们需要处理的就是onConversationLongClick()方法,首先把返回值更改为true,调用的时候就会调用我们自定义的弹框,而不是融云提供的
核心代码:
public class MyConversationListBehaviorListener implements RongIM.ConversationListBehaviorListener {/*** 当点击会话头像后执行。** @param context 上下文。* @param conversationType 会话类型。* @param targetId 被点击的用户id。* @return 如果用户自己处理了点击后的逻辑处理,则返回 true,否则返回 false,false 走融云默认处理方式。*/@Overridepublic boolean onConversationPortraitClick(Context context, Conversation.ConversationType conversationType, String targetId) {
// Toast.makeText(context,targetId,Toast.LENGTH_SHORT).show();return false;}/*** 当长按会话头像后执行。** @param context 上下文。* @param conversationType 会话类型。* @param targetId 被点击的用户id。* @return 如果用户自己处理了点击后的逻辑处理,则返回 true,否则返回 false,false 走融云默认处理方式。*/@Overridepublic boolean onConversationPortraitLongClick(Context context, Conversation.ConversationType conversationType, String targetId) {
// Toast.makeText(context,"长按 "+targetId,Toast.LENGTH_SHORT).show();return false;}/*** 长按会话列表中的 item 时执行。** @param context 上下文。* @param view 触发点击的 View。* @param uiConversation 长按时的会话条目。* @return 如果用户自己处理了长按会话后的逻辑处理,则返回 true, 否则返回 false,false 走融云默认处理方式。*/@Overridepublic boolean onConversationLongClick(Context context, View view, UIConversation uiConversation) {Conversation.ConversationType conversationType = uiConversation.getConversationType();//判断聊天类型,3:群聊、1:单聊等int conversationTypeValue = conversationType.getValue();//下面无需判断聊天类型
// String[] split = uiConversation.getConversationTargetId().split(Constant.TARGETID);
// int conversationTarget = Integer.parseInt(split[1]);//先获取单聊和群聊免打扰的状态值if (conversationTypeValue == 3) {//群聊RongIM.getInstance().getConversationNotificationStatus(Conversation.ConversationType.GROUP,uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {@Overridepublic void onSuccess(final Conversation.ConversationNotificationStatus conversationNotificationStatus) {final int value = conversationNotificationStatus.getValue();final Conversation.ConversationNotificationStatus conversationNotificationStatus1;if (value == 1) {conversationNotificationStatus1 = conversationNotificationStatus.setValue(0);disturb = getString(R.string.donot_disturb2);} else {conversationNotificationStatus1 = conversationNotificationStatus.setValue(1);disturb = getString(R.string.cancel_no_bother);}}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});} else if (conversationTypeValue == 1) {RongIM.getInstance().getConversationNotificationStatus(Conversation.ConversationType.PRIVATE,uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {@Overridepublic void onSuccess(final Conversation.ConversationNotificationStatus conversationNotificationStatus) {final int value = conversationNotificationStatus.getValue();final Conversation.ConversationNotificationStatus conversationNotificationStatus1;if (value == 1) {disturb = getString(R.string.donot_disturb2);} else {disturb = getString(R.string.cancel_no_bother);}}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});}//弹出的“保存图片”的DialogAlertDialog.Builder builder = new AlertDialog.Builder(getActivity());builder.setItems(new String[]{getString(R.string.pet_detail), disturb, getString(R.string.cancel)}, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {switch (which) {case 0:RongIM.getInstance().removeConversation(uiConversation.getConversationType(), uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Boolean>() {@Overridepublic void onSuccess(Boolean aBoolean) {
// RongIMClient.getInstance().deleteMessages(new int[]{uiConversation.get});RongIM.getInstance().removeConversation(uiConversation.getConversationType(), uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Boolean>() {@Overridepublic void onSuccess(Boolean aBoolean) {RongIM.getInstance().getConversationList(new RongIMClient.ResultCallback<List<Conversation>>() {@Overridepublic void onSuccess(List<Conversation> conversationeees) {//清除对应的用户对话记录RongIM.getInstance().clearMessages(uiConversation.getConversationType(), uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Boolean>() {@Overridepublic void onSuccess(Boolean aBoolean) {}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});break;case 1:if (conversationTypeValue == 3) {//群聊RongIM.getInstance().getConversationNotificationStatus(Conversation.ConversationType.GROUP,uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {@Overridepublic void onSuccess(final Conversation.ConversationNotificationStatus conversationNotificationStatus) {final int value = conversationNotificationStatus.getValue();final Conversation.ConversationNotificationStatus conversationNotificationStatus1;if (value == 1) {conversationNotificationStatus1 = conversationNotificationStatus.setValue(0);
// disturb = getString(R.string.donot_disturb2);} else {conversationNotificationStatus1 = conversationNotificationStatus.setValue(1);
// disturb = getString(R.string.cancel_no_bother);}RongIM.getInstance().setConversationNotificationStatus(Conversation.ConversationType.GROUP,uiConversation.getConversationTargetId(), conversationNotificationStatus1, new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {@Overridepublic void onSuccess(Conversation.ConversationNotificationStatus conversationNotificationStatus) {}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});} else if (conversationTypeValue == 1) {RongIM.getInstance().getConversationNotificationStatus(Conversation.ConversationType.PRIVATE,uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {@Overridepublic void onSuccess(final Conversation.ConversationNotificationStatus conversationNotificationStatus) {final int value = conversationNotificationStatus.getValue();final Conversation.ConversationNotificationStatus conversationNotificationStatus1;if (value == 1) {conversationNotificationStatus1 = conversationNotificationStatus.setValue(0);} else {conversationNotificationStatus1 = conversationNotificationStatus.setValue(1);}RongIM.getInstance().setConversationNotificationStatus(Conversation.ConversationType.PRIVATE,uiConversation.getConversationTargetId(), conversationNotificationStatus1, new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {@Overridepublic void onSuccess(Conversation.ConversationNotificationStatus conversationNotificationStatus) {}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});}break;case 2:break;}}});builder.show();//删除弹框提示
// final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
// builder.setTitle(getResources().getString(R.string.delete_sure));builder.setMessage("删除吗?");
// builder.setPositiveButton(getResources().getString(R.string.sure), new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// RongIM.getInstance().removeConversation(uiConversation.getConversationType(), uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Boolean>() {
// @Override
// public void onSuccess(Boolean aBoolean) {RongIMClient.getInstance().deleteMessages(new int[]{uiConversation.get});
// RongIM.getInstance().removeConversation(uiConversation.getConversationType(), uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Boolean>() {
// @Override
// public void onSuccess(Boolean aBoolean) {
// RongIM.getInstance().getConversationList(new RongIMClient.ResultCallback<List<Conversation>>() {
//
// @Override
// public void onSuccess(List<Conversation> conversationeees) {
//
// //清除对应的用户对话记录
// RongIM.getInstance().clearMessages(uiConversation.getConversationType(), uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Boolean>() {
// @Override
// public void onSuccess(Boolean aBoolean) {
//
// }
//
// @Override
// public void onError(RongIMClient.ErrorCode errorCode) {
//
// }
// });
//
//
// }
//
// @Override
// public void onError(RongIMClient.ErrorCode errorCode) {
//
// }
// });
//
// }
//
// @Override
// public void onError(RongIMClient.ErrorCode errorCode) {
//
// }
// });
//
// }
//
// @Override
// public void onError(RongIMClient.ErrorCode errorCode) {
//
// }
// });
// dialog.dismiss();
// }
// });
// builder.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// dialog.dismiss();
// }
// });
// AlertDialog alertDialog = builder.create();
// alertDialog.show();// RongIM.getInstance().removeConversation(uiConversation.getConversationType(),uiConversation.getConversationTargetId());//删除会话列表中的消息
// //删除会话列表
// RongIM.getInstance().removeConversation(Conversation.ConversationType.PRIVATE, uiConversation.getConversationTargetId());//个人 在自己删除对方好友关系
// RongIM.getInstance().getRongIMClient().removeConversation(Conversation.ConversationType.PRIVATE, split[1], new RongIMClient.ResultCallback<Boolean>() {
// @Override
// public void onSuccess(Boolean aBoolean) {
// Toast.makeText(context,aBoolean + "" ,Toast.LENGTH_SHORT).show();
// getData(conversationTarget);
// }
//
// @Override
// public void onError(RongIMClient.ErrorCode errorCode) {
//
// }
// });return true;}/*** 点击会话列表中的 item 时执行。** @param context 上下文。* @param view 触发点击的 View。* @param uiConversation 会话条目。* @return 如果用户自己处理了点击会话后的逻辑处理,则返回 true, 否则返回 false,false 走融云默认处理方式。*/@Overridepublic boolean onConversationClick(Context context, View view, UIConversation uiConversation) {return false;}}
上述代码就是实现单聊与群聊消息免打扰的设置,注释的代码仅供参考。
4.设置单聊与群聊的免打扰状态:
RongIM.getInstance().setConversationNotificationStatus(Conversation.ConversationType.GROUP,//群聊或单聊uiConversation.getConversationTargetId(),//群ID或单聊IDConversation.ConversationNotificationStatus.DO_NOT_DISTURB,//静音..打开改为NOTIFY即可new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() { //设置成功后的回调@Overridepublic void onSuccess(Conversation.ConversationNotificationStatus conversationNotificationStatus) {final int value = conversationNotificationStatus.getValue();final Conversation.ConversationNotificationStatus conversationNotificationStatus1;if(value==1){conversationNotificationStatus1=conversationNotificationStatus.setValue(0);disturb="免打扰";}else{conversationNotificationStatus1=conversationNotificationStatus.setValue(1);disturb="取消免打扰";}RongIM.getInstance().setConversationNotificationStatus(Conversation.ConversationType.GROUP,uiConversation.getConversationTargetId(), conversationNotificationStatus1, new RongIMClient.ResultCallback<Conversation.ConversationNotificationStatus>() {@Overridepublic void onSuccess(Conversation.ConversationNotificationStatus conversationNotificationStatus) {T.showShort("设置成功");}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {T.showShort("设置失败");}});}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});
二.单聊与群聊消息删除功能:
1.在对应的弹框中实现删除逻辑(上述核心代码中实现了两个简单的弹框,可以参考):
RongIM.getInstance().removeConversation(uiConversation.getConversationType(), uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Boolean>() {@Overridepublic void onSuccess(Boolean aBoolean) {
// RongIMClient.getInstance().deleteMessages(new int[]{uiConversation.get});RongIM.getInstance().removeConversation(uiConversation.getConversationType(), uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Boolean>() {@Overridepublic void onSuccess(Boolean aBoolean) {RongIM.getInstance().getConversationList(new RongIMClient.ResultCallback<List<Conversation>>() {@Overridepublic void onSuccess(List<Conversation> conversationeees) {//清除对应的用户对话记录RongIM.getInstance().clearMessages(uiConversation.getConversationType(), uiConversation.getConversationTargetId(), new RongIMClient.ResultCallback<Boolean>() {@Overridepublic void onSuccess(Boolean aBoolean) {}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});}@Overridepublic void onError(RongIMClient.ErrorCode errorCode) {}});
这篇关于Android 融云单聊与群聊消息免打扰功能设置与消息删除功能实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!