====== 多设备同步 ======
''本文档已不再维护,新版文档见:[[ccim:android:multidev|在多个设备上登录]]。''
-------
===== 服务设置 =====
* 默认设置:关闭
* 是否增值服务:是
===== 功能介绍 =====
- 开启多设备同步的APP可以支持同一个账号在同一端的多个设备上同时登录;
- 服务器提供查询每个账号已登录设备列表的接口,APP可通过接口强制账号从其他已登录设备中注销;
- 在同一账号所有已登录设备上同步在线消息和离线消息以及对应的回执和已读状态;
- 同一账号所有已登录的离线设备都可以接收推送;
- 在同一账号所有已登录设备上同步好友相关操作;
- 在同一账号所有已登录设备上同步群组、聊天室相关操作;
- 实时音视频不支持多设备同步。
===== 使用方法 =====
账号A同时在设备A和设备B上登录,账号A在设备A上进行一些操作,设备B上会收到这些操作对应的通知,具体说明如下
==== Android ====
EMMultiDeviceListener
interface EMMultiDeviceListener {
/**
* 好友已经在其他机子上被移除
*/
int CONTACT_REMOVE = 2;
/**
* 好友请求已经在其他机子上被同意
*/
int CONTACT_ACCEPT = 3;
/**
* 好友请求已经在其他机子上被拒绝
*/
int CONTACT_DECLINE = 4;
/**
* 当前用户在其他设备加某人进入黑名单
*/
int CONTACT_BAN = 5;
/**
* 好友在其他设备被移出黑名单
*/
int CONTACT_ALLOW = 6;
/**
* 创建了群组
*/
int GROUP_CREATE = 10;
/**
* 销毁了群组
*/
int GROUP_DESTROY = 11;
/**
* 已经加入群组
*/
int GROUP_JOIN = 12;
/**
* 已经离开群组
*/
int GROUP_LEAVE = 13;
/**
* 发起群组申请
*/
int GROUP_APPLY = 14;
/**
* 同意群组申请
*/
int GROUP_APPLY_ACCEPT = 15;
/**
* 拒绝群组申请
*/
int GROUP_APPLY_DECLINE = 16;
/**
* 邀请群成员
*/
int GROUP_INVITE = 17; //
/**
* 同意群组邀请
*/
int GROUP_INVITE_ACCEPT = 18; //
/**
* 拒绝群组邀请
*/
int GROUP_INVITE_DECLINE = 19;
/**
* 将某人踢出群
*/
int GROUP_KICK = 20;
/**
* 加入群组黑名单
*/
int GROUP_BAN = 21; //加入群组黑名单
/**
* 移除群组黑名单
*/
int GROUP_ALLOW = 22;
/**
* 屏蔽群组
*/
int GROUP_BLOCK = 23;
/**
* 取消群组屏蔽
*/
int GROUP_UNBLOCK = 24;
/**
* 转移群主
*/
int GROUP_ASSIGN_OWNER = 25;
/**
* 添加管理员
*/
int GROUP_ADD_ADMIN = 26;
/**
* 移除管理员
*/
int GROUP_REMOVE_ADMIN = 27;
/**
* 禁言用户
*/
int GROUP_ADD_MUTE = 28;
/**
* 移除禁言
*/
int GROUP_REMOVE_MUTE = 29;
/**
* 多设备联系人事件
*/
void onContactEvent(int event, String target, String ext);
/**
* 多设备群组事件
*/
void onGroupEvent(int event, String target, List usernames);
}
使用示例
//注册监听
EMClient.getInstance().addMultiDeviceListener(new MyMultiDeviceListener()); // class MyMultiDeviceListener implements EMMultiDeviceListener
//撤销监听
EMClient.getInstance().removeMultiDeviceListener(myMultiDeviceListener);
==== iOS ====
接口 EMMultiDevicesDelegate
/*!
* 多设备事件类型
* 用户UserA,登录2台机子DeviceA1和DeviceA2,另有一个用户UserB
*/
typedef NS_ENUM(NSInteger, EMMultiDevicesEvent) {
EMMultiDevicesEventUnknow = -1, // 默认
EMMultiDevicesEventContactRemove = 2, // UserB和UserA是好友,UserA在DeviceA1上删除了UserB,DeviceA2会收到该回调
EMMultiDevicesEventContactAccept = 3, // UserB向UserA发送加好友申请,UserA在DeviceA1上同意了该请求,DeviceA2会收到该回调
EMMultiDevicesEventContactDecline = 4, // UserB向UserA发送加好友申请,UserA在DeviceA1上拒绝了该请求,DeviceA2会收到该回调
EMMultiDevicesEventContactBan = 5, // UserA在DeviceA1上将UserB加入黑名单,DeviceA2会收到该回调
EMMultiDevicesEventContactAllow = 6, // UserA在DeviceA1上将UserB从黑名单中移除,DeviceA2会收到该回调
EMMultiDevicesEventGroupCreate = 10, // UserA在DeviceA1上创建了群组Group,DeviceA2会收到该回调
EMMultiDevicesEventGroupDestroy = 11, // UserA在DeviceA1上销毁了群组Group,DeviceA2会收到该回调
EMMultiDevicesEventGroupJoin = 12, // UserA在DeviceA1上主动加入了群组Group,DeviceA2会收到该回调
EMMultiDevicesEventGroupLeave = 13, // UserA在DeviceA1上退出了群组Group,DeviceA2会收到该回调
EMMultiDevicesEventGroupApply = 14, // UserA在DeviceA1上发送了申请进入Group,DeviceA2会收到该回调
EMMultiDevicesEventGroupApplyAccept = 15, // UserA收到UserB的入群申请,UserA在DeviceA1上同意了该申请,DeviceA2会收到该回调
EMMultiDevicesEventGroupApplyDecline = 16, // UserA收到UserB的入群申请,UserA在DeviceA1上拒绝了该申请,DeviceA2会收到该回调
EMMultiDevicesEventGroupInvite = 17, // UserA在DeviceA1上邀请了某些人进入GroupA,DeviceA2会收到该回调
EMMultiDevicesEventGroupInviteAccept = 18, //e UserBUserA加入群组,UserA在DeviceA1上同意了UserB的邀请,DeviceA2会收到该回调
EMMultiDevicesEventGroupInviteDecline = 19, // UserB邀请UserA加入群组,UserA在DeviceA1上拒绝了UserB的邀请,DeviceA2会收到该回调
EMMultiDevicesEventGroupKick = 20, // UserA在DeviceA1上将某些成员从GroupA中踢出,DeviceA2会收到该回调
EMMultiDevicesEventGroupBan = 21, // UserA在DeviceA1上将某些成员加入GroupA黑名单,DeviceA2会收到该回调
EMMultiDevicesEventGroupAllow = 22, // UserA在DeviceA1上将某些成员从GroupA黑名单中移除,DeviceA2会收到该回调
EMMultiDevicesEventGroupBlock = 23, // UserA在DeviceA1上屏蔽了GroupA的消息,DeviceA2会收到该回调
EMMultiDevicesEventGroupUnBlock = 24, // UserA在DeviceA1上取消了屏蔽GroupA的消息,DeviceA2会收到该回调
EMMultiDevicesEventGroupAssignOwner = 25, // UserA在DeviceA1上更新了GroupA的群主,DeviceA2会收到该回调
EMMultiDevicesEventGroupAddAdmin = 26, // UserA在DeviceA1上添加了GroupA的管理员,DeviceA2会收到该回调
EMMultiDevicesEventGroupRemoveAdmin = 27, // UserA在DeviceA1上移除了GroupA的管理员,DeviceA2会收到该回调
EMMultiDevicesEventGroupAddMute = 28, // UserA在DeviceA1上禁言了GroupA的某些成员,DeviceA2会收到该回调
EMMultiDevicesEventGroupRemoveMute = 29, // UserA在DeviceA1上移除了GroupA的某些禁言成员,DeviceA2会收到该回调
};
/*!
* 好友多设备事件回调
*
* @param aEvent 多设备事件类型
* @param aUsername 用户名
* @param aExt 扩展信息
*/
- (void)multiDevicesContactEventDidReceive:(EMMultiDevicesEvent)aEvent
username:(NSString *)aUsername
ext:(NSString *)aExt;
/*!
* 群组多设备事件回调
*
* @param aEvent 多设备事件类型
* @param aGroupId 群组ID
* @param aExt 扩展信息, 是被操作对象的数组(NSMutableArray)
*/
- (void)multiDevicesGroupEventDidReceive:(EMMultiDevicesEvent)aEvent
groupId:(NSString *)aGroupId
ext:(id)aExt;
使用示例
//注册监听
[[EMClient sharedClient] addMultiDevicesDelegate:aDelegate delegateQueue:aQueue];
//监听回调
- (void)multiDevicesContactEventDidReceive:(EMMultiDevicesEvent)aEvent
username:(NSString *)aTarget
ext:(NSString *)aExt
{
NSString *message = [NSString stringWithFormat:@"%li-%@-%@", (long)aEvent, aTarget, aExt];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert.multi.contact", @"Contact Multi-devices") message:message delegate:self cancelButtonTitle:NSLocalizedString(@"ok", @"OK") otherButtonTitles:nil, nil];
[alertView show];
}
- (void)multiDevicesGroupEventDidReceive:(EMMultiDevicesEvent)aEvent
groupId:(NSString *)aGroupId
ext:(id)aExt
{
NSString *message = [NSString stringWithFormat:@"%li-%@-%@", (long)aEvent, aGroupId, aExt];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert.multi.group", @"Group Multi-devices") message:message delegate:self cancelButtonTitle:NSLocalizedString(@"ok", @"OK") otherButtonTitles:nil, nil];
[alertView show];
}
===== 典型应用:PC与移动端互发消息 =====
当PC端和手机端登录同一个账号时,在手机端可以通过特定方法获取到PC端的设备ID,该设备ID相当于特殊的好友Username,可以直接使用于聊天,使用方法与好友类似。
==== Android ====
List selfIds = EMClient.getInstance().contactManager().getSelfIdsOnOtherPlatform();
==== iOS ====
NSArray *otherPlatformIds = [[EMClient sharedClient].contactManager getSelfIdsOnOtherPlatformWithError:nil];
if ([otherPlatformIds count] > 0) {
NSString *chatter = otherPlatformIds[0];
//获取会话
EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:chatter type:EMConversationTypeChat createIfNotExist:YES];
//发送消息
NSString *sendText = @"test";
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:sendText];
NSString *from = [[EMClient sharedClient] currentUsername];
EMMessage *message = [[EMMessage alloc] initWithConversationID:conversation.conversationId from:from to:chatter body:body ext:nil];
message.chatType = EMChatTypeChat;
[[EMClient sharedClient].chatManager sendMessage:message progress:nil completion:nil];
}
----
上一页:[[im:extensions:live:demo|Demo功能介绍和使用]]
下一页:[[im:extensions:value:messageroaming|消息漫游]]