一、前言
这功能也属于比较常见,比如用户发了超过多少次数,不给发了,需要收费。或者不给用户发照片,或者检测用户发的是什么内容,是否合规。
二、效果图
好像这没法有效果图
三、解决思路
融云SDK拦截的监听是RongIM.getInstance().setMessageInterceptor方法。关键是请求app接口都是异步的,请求接口没回来,就需要返回true或者false了。
我的解决方案:请求接口先返回true,然后在extra做处理(关键),能发送再帮用户发一次。
四、参考示例代码
RongIM.getInstance().setMessageInterceptor(object :MessageInterceptor{
override fun interceptReceivedMessage(
p0: Message?,
p1: Int,
p2: Boolean,
p3: Boolean
): Boolean {
return false
}
override fun interceptOnSendMessage(message: Message?): Boolean {
LogUtils.o("interceptOnSendMessage","interceptOnSendMessage")
message?.let {
//发送前是还没有uid的
//LogUtils.o("interceptOnSendMessage",it.uId)
LogUtils.o("interceptOnSendMessage",message)
return if (message.extra == "isCanSend"){
false
} else{
//context可能为null
if (context == null){
false
} else {
//这里是请求接口
ImViewModel(CommonRepository()).postChatSendCheck(context!!,toUserId,message)
true
}
}
}
return false
}
override fun interceptOnSentMessage(message: Message?): Boolean {
return false
}
})
请求接口后帮用户重发:
viewModelScope.launch(Dispatchers.Main){
val result = repo.postChatSendCheck(toUserId,type, content,sendNum,currentStamp)
if (result is RResult.Success ) {
when(result.code){
BaseResultCodes.RESULT_OK -> {
//可以发送
message.extra = "isCanSend"
if (mc is HQVoiceMessage || mc is ImageMessage || mc is SightMessage || mc is GIFMessage ){
RongIM.getInstance().sendMediaMessage(message,message.messagePushConfig?.pushContent?:"",message.messagePushConfig?.pushData?:"",object :
IRongCallback.ISendMediaMessageCallback{
override fun onAttached(p0: Message?) {
}
override fun onSuccess(p0: Message?) {
}
override fun onError(p0: Message?, p1: RongIMClient.ErrorCode?) {
}
override fun onProgress(p0: Message?, p1: Int) {
}
override fun onCanceled(p0: Message?) {
}
})
} else {
RongIM.getInstance().sendMessage(message,message.messagePushConfig?.pushContent?:"",message.messagePushConfig?.pushData?:"",null)
}
有疑问可私信我,融云其他功能也可以。
网友评论