消息回撤:当A用户发消息给B用户后,可以在一段时间内将消息回撤。
A用户发消息给B用户,当需要回撤的时候,将要回撤消息的id通过扩展消息发送给B,B检测到回撤的透传消息后,将对应messageid的消息从数据库删除。
注:“扩展消息”指透传消息,它是一种特殊类型的消息,收发双方不会存数据库,同时用户离线时也不会有推送,比较适合配合业务来处理一些功能。
EMMessage cmdMsg = EMMessage.createSendMessage(EMMessage.Type.CMD);
// 如果是群聊,设置chattype,默认是单聊
if (chatType == CHATTYPE_GROUP){
cmdMsg.setChatType(ChatType.GroupChat);
}
String action="REVOKE_FLAG";
CmdMessageBody cmdBody=new CmdMessageBody(action);
// 设置消息body
cmdMsg.addBody(cmdBody);
// 设置要发给谁,用户username或者群聊groupid
cmdMsg.setReceipt(toChatUsername);
// 通过扩展字段添加要撤回消息的id
cmdMsg.setAttribute("msgId",msgid);
EMChatManager.getInstance().sendMessage(cmdMsg,new EMCallBack() {
@Override
public void onSuccess() {}
@Override
public void onProgress(int progress, String status) {}
@Override
public void onError(int code, String error) {}
});
- (void)revokeMessageWithMessageId:(NSString *)aMessageId{
EMChatCommand *revokeCommand = [[EMChatCommand alloc] init];
revokeCommand.cmd = @"REVOKE_FLAG"; // 当前cmd消息的关键字
EMCommandMessageBody *revokeMsgBody = [[EMCommandMessageBody alloc] initWithChatObject:revokeCommand];
// 设置要发给谁,用户username或者群聊groupid
EMMessage *revokeMsg = [[EMMessage alloc] initWithReceiver:@"" bodies:@[revokeMsgBody]];
revokeMsg.messageType = eMessageTypeChat; // 单聊或者群聊
revokeMsg.ext = @{@"msgId":aMessageId};
[[EaseMob sharedInstance].chatManager asyncSendMessage:revokeMsg progress:nil];
}
EMChatManager.getInstance().registerEventListener(new EMEventListener() {
@Override
public void onEvent(EMNotifierEvent event) {
switch (event.getEvent()) {
case EventNewCMDMessage: // CMD消息
{
EMMessage message = (EMMessage) event.getData();
CmdMessageBody cmdMsgBody = (CmdMessageBody) message.getBody();
String action = cmdMsgBody.action;//获取自定义action
if(action.equals("REVOKE_FLAG")){
try {
String msgId = message.getStringAttribute("msgId");
EMConversation conversation = EMChatManager.getInstance().getConversation(message.getFrom());
--删除消息来表示撤回--
conversation.removeMessage(msgId);
// 如果需要,可以插入一条“XXX回撤一条消息”
} catch (EaseMobException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
default:
break;
}
}
}, new EMNotifierEvent.Event[] { EMNotifierEvent.Event.EventNewCMDMessage});
// 接收在线透传消息
- (void)didReceiveCmdMessage:(EMMessage *)cmdMessage{
EMCommandMessageBody *body = cmdMessage.messageBodies.firstObject;
if ([body.action isEqualToString:REVOKE_FLAG]) {
[self removeRevokeMessageWithChatter:cmdMessage.from
conversationType:(EMConversationType)cmdMessage.messageType
messageId:cmdMessage.ext[@"msgId"]];
}
}
// 接收离线透传消息
- (void)didReceiveOfflineCmdMessages:(NSArray *)offlineCmdMessages{
for (EMMessage *cmdMessage in offlineCmdMessages) {
[self didReceiveCmdMessage:cmdMessage];
}
}
// 删除消息
- (void)removeRevokeMessageWithChatter:(NSString *)aChatter
conversationType:(EMConversationType)type
messageId:(NSString *)messageId{
EMConversation *conversation = [[EaseMob sharedInstance].chatManager conversationForChatter:aChatter
conversationType:type];
BOOL success = [conversation removeMessageWithId:messageId];
if (success) {
// update ui,如果需要,可以插入一条“XXX回撤一条消息”
}
}