开发手册

SDK介绍

HTMessage SDK 为用户开发IM相关的应用提供的一套完善的开发框架。包括两个部分:

  • HTMessage SDK Framework,SDK内核

  • 基于HTMessage SDK开发的一套高仿环信的Demo

HTClient: 是SDK的入口,主要完成登录、退出、连接管理,以及消息发送等功能。也是获取其他模块的入口

HTMessageManager:管理消息存储以及接收消息的监听

HTConversationManager:管理会话列表的相关操作

HTGroupManager:管理群组的相关操作

SDK的集成

将从官网下载HTMessageSDKFramework拖入到项目中 

向Build Phases → Link Binary With Libraries 中添加依赖库,其中包括Demo中使用到的依赖库

  • SDK 不支持 bitcode,向 Build Settings → Linking → Enable Bitcode 中设置 NO
  • 以上步骤进行完后,编译工程。如果没有报错,恭喜你,集成 SDK 成功,可以进行下一步了。

SDK快速集成

1 初始化SDK

  • 引入头文件

    #import <HTMessageSDKFramework/HTMessageSDKFramework.h>
  • 初始化SDK

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
          ...
          [[HTClient sharedInstance] initializeSDK];
          ...
          return YES;
      }
  • 登录HTMessage

    [[HTClient sharedInstance] loginWithUsername:@"10000" password:@"1234" completion:^(BOOL result) {
          if (result) {
              NSLog(@"登陆成功");
          }else{
              NSLog(@"登陆成功");
          }
      }];
  • 退出HTMessage

    [[HTClient sharedInstance] logout];
  • 初始化单聊页面

    HTChatViewController *HTChatVC = [[HTChatViewController alloc] initWithConversationChatter:@"100001" conversationType:@"1"];
  • 创建群组

    HTGroup * group = [HTGroup new];
      gModel.groupName = @"群名称";
      gModel.groupDescription = @"群描述";
      gModel.groupAvatar = @"群头像";
      [[HTClient sharedInstance].groupManager createGroupWithGroupModel:group withMembers:members success:^(HTGroup *aGroup) {
    
      } failure:^(NSError *error) {
    
      }];
  • 初始化群聊界面

    HTChatViewController *HTChatVC = [[HTChatViewController alloc] initWithConversationChatter:@"groupId" conversationType:@"1"];

消息

  • 文字消息

    HTMessage *message = [HTMessageSDKHelper sendTextMessage:@"文本内容" to:@"发送对象" messageType:@"聊天类型" messageExt:@"拓展内容"];
      messageType:
                   @"1" 为单聊消息
                   @"2" 为群聊消息
      messageExt:
                   @{}  拓展内容为字典类型,可传nil
  • 图片消息

    HTMessage *message = [HTMessageSDKHelper sendImageMessageWithImage:@"图片UIImage对象" to:@"发送对象" messageType:@"聊类型" messageExt:@"拓展内容"];
  • 语音消息

    HTMessage *message = [HTMessageSDKHelper sendAudioMessageWithLocalPath:@"amr格式语音文件路径" duration:@"语音时长" to:@"发送对象" messageType:@"聊天类型" messageExt:@"拓展内容"];
  • 视频消息

    HTMessage * message = [HTMessageSDKHelper sendVideoMessageWithURL:@"mp4格式视频文件路径" to:@"发送对象" messageType:@"聊天类型" andSSImage:@"视频截图UIImage对象" andVideoTime:@"视频时长" messageExt:@"拓展字段"];
  • 位置消息

    HTMessage * message = [HTMessageSDKHelper sendLocationMessageWithLatitude:@"维度" longitude:@"经度" address:@"地址详情" to:@"聊天对象" messageType:@"聊天类型" andSSImage:@"地图截图UIImage对象" messageExt:@"拓展字段"];
  • 发送消息

    [[HTClient sharedInstance] sendMessage:message completion:^(HTMessage *message, NSError *error) {
          if (error) {
              NSLog(@"error = %@",error);
          }
      }];
  • 接收消息

    1、注册代理"HTMessageDelegate"
      2、实现方法
    
       接收到普通消息
      - (void)didReceiveMessages:(NSArray *)aMessages;
    
       接收到透传消息
      - (void)didReceiveCMDMessage:(NSArray *)aCMDMessages;

集成准备

  1. libs中添加htmessage.jar

  2. gradle中添加依赖包:


dependencies {
   
    compile 'com.anupcowkur:reservoir:3.1.0'
    compile 'com.squareup.okhttp3:okhttp:3.3.1' 
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.1.6'
    compile 'com.android.support:support-v4:25.0.0'
    compile 'com.segment.backo:backo:1.0.0'
    compile 'com.jaredrummler:android-processes:1.0.9'
    compile 'com.alibaba:fastjson:1.2.24'
    compile 'com.alibaba:fastjson:1.1.56.android'
}

初始化SDK

     1.在Application类中添加初始化SDK的代码:

@Override
public void onCreate() {
    ......
    
    HTClient.init(applicationContext);
    
    ......
}

注册IM

HTClient.getInstance().register(username, password, new HTClient.HTCallBack() {
//异步回调注册结果
    @Override
    public void onSuccess() {
         //注册成功
    }

    @Override
    public void onError() {
         //注册失败
     }
});

登录IM服务器

HTClient.getInstance().login(username, password, new HTClient.HTCallBack() {
//异步回调登录结果
    @Override
    public void onSuccess() {
         //登录成功
    }

    @Override
    public void onError() {
         //登录失败
     }
});

退出IM服务器

HTClient.getInstance().logout(new HTClient.HTCallBack() {
 //异步回调
            @Override
            public void onSuccess() {
 
                 //退出成功
            }

            @Override
            public void onError() {
                 //退出失败
            }
        });

发送消息

HTClient.getInstance().chatManager().sendMessage(htMessage, new HTChatManager.HTMessageCallBack() {
//异步回调
        @Override
        public void onProgress(  ) {
            //发送中

        }

        @Override
        public void onSuccess(   ) {
            //发送成功
        }

        @Override
        public void onFailure(   ) {
            //发送失败
        }

    });


}

接收消息监听

private HTClient.MessageLisenter messageLisenter = new HTClient.MessageLisenter() {
    @Override
    public void onHTMessage(HTMessage htMessage) {
       //收到普通消息---文本,语音,图片,视频,位置等
    }

    @Override
    public void onCMDMessgae(CmdMessage cmdMessage) {

       //收到透传消息

    }
};

注册监听

HTClient.getInstance().setMessageLisenter(messageLisenter);

获取一个群实体

HTGroup htgroup=GroupManager.getInstance().getGroup(groupId);

创建群组

GroupManager.getInstance().createGroup(members, groupName, groupDsc,groupImage, new GroupManager.CallBack() {
    @Override
    public void onSuccess(String data) {
         
        }
    @Override
    public void onFailure() {
         
    }
});

群踢人

HTClient.getInstance().groupManager().deleteMember(groupId, memberUserId,memberUserNick, new GroupManager.CallBack() {
        @Override
        public void onSuccess(String data) {
           //成功
        }

        @Override
        public void onFailure() {
            //失败

        }
    });
}

群主删除群

HTClient.getInstance().groupManager().deleteGroup(groupId, new GroupManager.CallBack() {
    @Override
    public void onSuccess(String data) {
        //
    }

    @Override
    public void onFailure() {
        //
    }
});

群成员退出群

HTClient.getInstance().groupManager().leaveGroup(groupId, HTApp.getInstance().getUserJson().getString(HTConstant.JSON_KEY_NICK), new GroupManager.CallBack() {
    @Override
    public void onSuccess(String data) {
        
    }

    @Override
    public void onFailure() {
         
    }
});

群加人

GroupManager.getInstance().addMembers(map, groupId, new GroupManager.CallBack() {
            @Override
            public void onSuccess(String data) {
                
            }

            @Override
            public void onFailure() {
                
            }
        });
    }

更新群资料

GroupManager.getInstance().updateGroupInfo(groupId, groupNewName, groupDesc, imageUrl, new GroupManager.CallBack() {
    @Override
    public void onSuccess(String data) {
       
    }

    @Override
    public void onFailure() {
        
    }
});

电话联系我们0551-66771882

主题颜色