环信客服的问候语直接打开开关就可以,但是欢迎语需要自己去掉环信的接口获取。
获取欢迎语的思路是,通过环信的接口获取欢迎语,然后拿到欢迎语封装成一条消息,导入数据库中,刷新tableView即可。
具体的步骤:(先贴代码,具体看附件,先把附件的1,2步骤完成)
1.通过post请求先获取管理员的token
// 自己封装好一个方法 获取token 这里采用的是AFNetworking
+ (void)HXGetTokenWithReturnValeuBlock: (HXGetTokenSuccessBlock) successblock WithFailureBlock: (FailureBlock) failureBlock
{
NSURL *url = [NSURL URLWithString:@"https://a1.easemob.com/下图中的“组织”/下图中的应用名称/token"];
NSDictionary *body = @{@"grant_type":@"password",@"username": “手机app登录的用户名”,@"password":@"环信密码"};
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[[self DictionaryConversionJson:body] dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPSessionManager *manager = [self manager];
SKURLSessionTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (responseObject==nil) {
return;
}
id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"服务器返回信息%@",obj);
if (obj==nil) {
return;
}
NSString* token = [obj safeJsonObjForKey:@"access_token"];
successblock(token);
}];
[task resume];
}
+(NSString*)DictionaryConversionJson:(NSDictionary *)Dictionary
{
NSError *parseError = nil;
NSString *jsonStr = @"";
if ([Dictionary isKindOfClass:[NSDictionary class]]){
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:Dictionary options:NSJSONWritingPrettyPrinted error:&parseError];
jsonStr = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
}
return jsonStr;
}
2.获取token后,再去获取欢迎语
+ (void)HXGetWelComeStrWithToken:(NSString *)token ReturnValeuBlock: (HXGetTokenSuccessBlock) successblock WithFailureBlock: (FailureBlock) failureBlock;
{
NSString *userName = USER_NAME;
NSString *url = [NSString stringWithFormat:@"http://kefu.easemob.com/v1/tenantapi/welcome?tenantId=“填写tenantId”&orgName=“填写组织”&appName=“填写应用名称”&userName=%@&token=%@",userName,token];
AFHTTPSessionManager *manager = [self manager];
[manager GET:url parameters:@"" progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id obj = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"服务器返回信息%@",obj);
successblock(obj);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failureBlock(error.code);
}];
}
3.获取到欢迎语后构造一条消息,我们的聊天界面采用的是环信的EaseMessageViewController
所以在viewDidLoad里面去构造这个消息
// 获取管理员的token
[SKRequest HXGetTokenWithReturnValeuBlock:^(NSString *token) {
//获取欢迎语
[SKRequest HXGetWelComeStrWithToken:token ReturnValeuBlock:^(NSString *token) {
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:token];
NSString *from = [[EMClient sharedClient] currentUsername];
//生成Message
EMMessage *message = [[EMMessage alloc] initWithConversationID:@"填写IM服务号,注意全部小写" from:from to:@"填写IM服务号,注意全部小写" body:body ext:nil];
message.chatType = EMChatTypeChat;// 设置为单聊消息
message.status=EMMessageStatusSuccessed;
message.direction = EMMessageDirectionReceive;
EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:from type:EMConversationTypeChat createIfNotExist:YES];
[conversation insertMessage:message];
[self.tableView reloadData];
} WithFailureBlock:^(NSInteger errorCode) {
}];
} WithFailureBlock:^(NSInteger errorCode) {
}];
4.完成