消息回撤
消息回撤:当A用户发消息给B用户后,可以在一段时间内将消息回撤。
A用户发消息给B用户,当需要回撤的时候,将要回撤消息的id通过扩展消息发送给B,B检测到回撤的透传消息后,将对应messageid的消息从数据库删除。
- A用户发送消息。
- A用户需要撤回某条消息,将消息id通过扩展消息发送到用户B。
- B用户收到扩展消息,解析其中的messageid,从数据库删除对应消息。
注:“扩展消息”指透传消息,它是一种特殊类型的消息,收发双方不会存数据库,同时用户离线时也不会有推送,比较适合配合业务来处理一些功能。
发送要回撤的透传消息
Android示例
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) {}
});
iOS示例
- (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];
}
接收透传信息,删除要回撤的消息
Android示例
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});
iOS示例
// 接收在线透传消息
- (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回撤一条消息”
}
}