Android 融云单聊与群聊消息免打扰功能设置与消息删除功能实现

2023-11-29 09:18

本文主要是介绍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 融云单聊与群聊消息免打扰功能设置与消息删除功能实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/432269

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构