多设备


当PC端和手机端登录同一个账号时,在手机端可以通过特定方法获取到PC端的设备ID,该设备ID相当于特殊的好友Username,可以直接使用于聊天,使用方法与好友类似。

接口

/*!
 *  \~chinese
 *  获取当前账号在其他平台(Windows或者Web)登录的id列表
 *  id使用方法类似于好友username
 *
 *  @param pError   错误信息
 *
 *  @return     id列表
 *
 *  \~english
 *  Get the id list of the current account on another platform (Windows or Web)
 *  Id usage is similar to friend username
 *
 *  @param pError   Error
 *
 *  @return     id list
 *
 */
- (NSArray *)getSelfIdsOnOtherPlatformWithError:(EMError **)pError;

/*!
 *  \~chinese
 *  获取当前账号在其他平台(Windows或者Web)登录的id列表
 *  id使用方法类似于好友username
 *
 *  @param aCompletionBlock 完成的回调
 *
 *  \~english
 *  Get the id list of the current account on another platform (Windows or Web)
 *  Id usage is similar to friend username
 *
 *  @param aCompletionBlock The callback block of completion
 *
 */
- (void)getSelfIdsOnOtherPlatformWithCompletion:(void (^)(NSArray *aList, EMError *aError))aCompletionBlock;

使用示例

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];
}

账号A同时在设备A和设备B上登录,账号A在设备A上进行一些操作,设备B上会收到这些操作对应的通知,具体说明如下:

接口 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];
}