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

相关文章

Java实现MD5加密的四种方式

《Java实现MD5加密的四种方式》MD5是一种广泛使用的哈希算法,其输出结果是一个128位的二进制数,通常以32位十六进制数的形式表示,MD5的底层实现涉及多个复杂的步骤和算法,本文给大家介绍了Ja... 目录MD5介绍Java 中实现 MD5 加密方式方法一:使用 MessageDigest方法二:使用

mysql删除无用用户的方法实现

《mysql删除无用用户的方法实现》本文主要介绍了mysql删除无用用户的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 1、删除不用的账户(1) 查看当前已存在账户mysql> select user,host,pa

Nginx配置location+rewrite实现隐性域名配置

《Nginx配置location+rewrite实现隐性域名配置》本文主要介绍了Nginx配置location+rewrite实现隐性域名配置,包括基于根目录、条件和反向代理+rewrite配置的隐性... 目录1、配置基于根目录的隐性域名(就是nginx反向代理)2、配置基于条件的隐性域名2.1、基于条件

Linux配置IP地址的三种实现方式

《Linux配置IP地址的三种实现方式》:本文主要介绍Linux配置IP地址的三种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录环境RedHat9第一种安装 直接配置网卡文件第二种方式 nmcli(Networkmanager command-line

Java实现将Markdown转换为纯文本

《Java实现将Markdown转换为纯文本》这篇文章主要为大家详细介绍了两种在Java中实现Markdown转纯文本的主流方法,文中的示例代码讲解详细,大家可以根据需求选择适合的方案... 目录方法一:使用正则表达式(轻量级方案)方法二:使用 Flexmark-Java 库(专业方案)1. 添加依赖(Ma

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis

Mybatis从3.4.0版本到3.5.7版本的迭代方法实现

《Mybatis从3.4.0版本到3.5.7版本的迭代方法实现》本文主要介绍了Mybatis从3.4.0版本到3.5.7版本的迭代方法实现,包括主要的功能增强、不兼容的更改和修复的错误,具有一定的参考... 目录一、3.4.01、主要的功能增强2、selectCursor example3、不兼容的更改二、

如何使用C#串口通讯实现数据的发送和接收

《如何使用C#串口通讯实现数据的发送和接收》本文详细介绍了如何使用C#实现基于串口通讯的数据发送和接收,通过SerialPort类,我们可以轻松实现串口通讯,并结合事件机制实现数据的传递和处理,感兴趣... 目录1. 概述2. 关键技术点2.1 SerialPort类2.2 异步接收数据2.3 数据解析2.

mybatis-plus 实现查询表名动态修改的示例代码

《mybatis-plus实现查询表名动态修改的示例代码》通过MyBatis-Plus实现表名的动态替换,根据配置或入参选择不同的表,本文主要介绍了mybatis-plus实现查询表名动态修改的示... 目录实现数据库初始化依赖包配置读取类设置 myBATis-plus 插件测试通过 mybatis-plu

Qt把文件夹从A移动到B的实现示例

《Qt把文件夹从A移动到B的实现示例》本文主要介绍了Qt把文件夹从A移动到B的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录如何移动一个文件? 如何移动文件夹(包含里面的全部内容):如何删除文件夹:QT 文件复制,移动(