输入状态提示:当A用户正在输入文字的时候,聊天的对方可以收到用户A正在输入的提示。
监听用户A输入状态,一旦有文字输入,就每隔几秒通过透传消息将输入状态发送到聊天接受方B,B客户端收到输入状态透传后,提示用户A正在输入。
注:“透传消息”是一种特殊类型的消息,收发双方不会存数据库,同时用户离线时也不会有推送,比较适合配合业务来处理一些功能。
注意:
A用户不需要不停的向B发送消息,可以设置一个时间,判断发送间隔。
public int time = 5000;//按照自己的需求自定义时间间隔发送状态消息(单位:ms)
public long firstTime = System.currentTimeMillis();//文本框第一次输入内容变化的时间
EditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (System.currentTimeMillis()-firstTime > time) {
sendInputmsg();
firstTime = System.currentTimeMillis();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
public void sendInputmsg(){
EMMessage cmdMsg = EMMessage.createSendMessage(EMMessage.Type.CMD);
// 如果是群聊,设置chattype,默认是单聊
if (chatType == CHATTYPE_GROUP){
cmdMsg.setChatType(ChatType.GroupChat);
}
String action="input";
CmdMessageBody cmdBody=new CmdMessageBody(action);
// 设置消息body
cmdMsg.addBody(cmdBody);
// 设置要发给谁,用户username或者群聊groupid
cmdMsg.setReceipt(toChatUsername);
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) {}
});
}
#define TIME_FLAG 5 // 时间阀值
#define INPUT_FLAG @"input" // 透传消息关键字
long long _time = 0;
- (void)sendInputMsg:(NSString *)conversationId {
if ([self isCanSendInputMsg]) {
EMCmdMessageBody *body = [[EMCmdMessageBody alloc] initWithAction:INPUT_FLAG];
NSString *currentUsername = [EMClient sharedClient].currentUsername;
EMMessage *inputMsg = [[EMMessage alloc] initWithConversationID:conversationId
from:currentUsername
to:conversationId
body:body
ext:nil];
inputMsg.chatType = EMChatTypeChat; // 单聊
//发送cmd消息
[[EMClient sharedClient].chatManager sendMessage:inputMsg
progress:nil
completion:^(EMMessage *message, EMError *error) {
if (!error) {
NSLog(@"发送成功");
}
}
];
}
}
- (BOOL)isCanSendInputMsg{
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
// _time 全局变量,记录时间
if (currentTime - _time > TIME_FLAG) {
_time = [[NSDate date] timeIntervalSince1970];
return YES;
}
return NO;
}
// UITextView回调方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
[self sendInputMsg:self.conversation.conversationId];
return YES;
}
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 aciton = cmdMsgBody.action;//获取自定义action
if(action.equals("input")){
//update UI
}
break;
}
default:
break;
}
}
}, new EMNotifierEvent.Event[] { EMNotifierEvent.Event.EventNewCMDMessage});
- (void)cmdMessagesDidReceive:(NSArray *)aCmdMessages {
[aCmdMessages enumerateObjectsWithOptions:NSEnumerationReverse
usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[EMMessage class]]) {
EMMessage *cmdMessage = (EMMessage *)obj;
EMCmdMessageBody *body = (EMCmdMessageBody *)cmdMessage.body;
if ([body.action isEqualToString:@"input"]) {
*stop = YES;
// update ui
}
}
}
];
}