为了账号安全,请及时绑定邮箱和手机立即绑定

用腾讯云通信做聊天功能遇到的问题

标签:
iOS

一.登录问题

用户登录腾讯后台服务器后才能正常收发消息,登录需要用户提供 UserID、UserSig。

TIMLoginParam *param = [[TIMLoginParam alloc] init];
    param.identifier = tencentAccount;
    param.userSig = userSig;
    [[TIMManager sharedInstance] login:param succ:^{
        NSLog(@"腾讯云登录成功"); 
 } fail:^(int code, NSString *msg) {
        [WHToast showMessage:msg duration:2.0f finishHandler:nil];
    }];

tencentAccount和userSig分别代表腾讯云账号和签名,是服务端返回的字段。当用户票据过期或此用户在其他终端被踢,登录将会失败,前端就需要重新登录。在启动APP时添加观察者监听用户登录状态。

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserStatus:) name:TUIKitNotification_TIMUserStatusListener object:nil];//监听用户登录状态

- (void)onUserStatus:(NSNotification *)notification
{
    TUIUserStatus status = [notification.object integerValue];
    __weak typeof(self) ws = self;
    if(status==TUser_Status_ForceOffline||status==TUser_Status_SigExpired){
        UIAlertController *alert = [IHUtility createAlertWithTitle:
@"下线通知" message:@"您的帐号于另一台手机上登录。" confirmAction:@"重新登录"  preferred:UIAlertControllerStyleAlert confirmHandler:^(UIAlertAction *confirmAction) {
            [ws gotoLogin];//跳到登录页,重新登录
        } cancleHandler:^(UIAlertAction *cancleAction) {
        }];
        [self presentViewController:alert animated:YES completion:nil];
    }else {
         [WHToast showMessage:@"网络异常" duration:2.0f finishHandler:nil];
    }
}

二.登录退出,清空聊天记录。

在退出切换账号时,需要调取腾讯云的logout方法,这样才会清楚当前登录用户的聊天列表。

[[TIMManager sharedInstance] logout:^() {
    NSLog(@"logout succ");
} fail:^(int code, NSString * err) {
    NSLog(@"logout fail: code=%d err=%@", code, err);
}];

需要 logout 回调成功或者失败后才能再次 login,否则 login 可能会失败。

三.用户置顶

需求:易脚官方消息通过管理员发消息的方式推送,需要置顶。
图片描述
设置:在会话列表中根据不同的用户ID设置需要置顶的用户。

 _viewModel.listFilter = ^BOOL(TUIConversationCellData * _Nonnull data) {
            if([data.convId isEqualToString:@"storeAdmin"]||[data.convId isEqualToString:@"admin"]){
                //置顶
                [[TUILocalStorage sharedInstance] addTopConversation:@"storeAdmin"];
                [[TUILocalStorage sharedInstance] addTopConversation:@"admin"];
        }

3.添加自定义信息(工号,手机号)
8284工号
需求:聊天时需要显示技师工号,及点头像需要传入手机号码,就需要自定义字段。
设置:在腾讯云控制台加入相对应的字段,打开ChatViewController页面,获取设置的自定义字段。
图片描述
关键代码:

//获取指定用户资料
  [[TIMFriendshipManager sharedInstance] getUsersProfile:@[_conversationData.convId] forceUpdate:NO succ:^(NSArray * arr) {
        for (TIMUserProfile * profile in arr) {
              [weakSelf setTitle:[IHUtility hexStringFromString:profile.customInfo[@"jobNum"]] color:titleColor];
            _chat.customInfo = profile.customInfo;
        }
    }fail:^(int code, NSString * err) {
        NSLog(@"GetFriendsProfile fail: code=%d err=%@", code, err);
    }];

+ (NSString *)hexStringFromString:(NSData *)data{
    NSString *hexStr=[[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
    return hexStr;
}

图片描述

因为自定义字段集合,key 是 NSString 类型,value 是 NSData 类型或者 NSNumber 类型,所以需要转码。

四.聊天时获取用户头像

腾讯云通信比较坑的一点,在聊天时没有设置用户头像,需要开发者自己设置,因为目前项目中只涉及到单聊,所以设置比较简单,想着好友头像从聊天列表页面传值给聊天页面,自己头像直接从登录后缓存中取出。打开TUIMessageController页面,添加代码。

   data.userAvatarUrl = [NSURL URLWithString:USERMODEL.headImg];
   data.friendAvatarUrl = self.friendHeader;

//UI 赋值
 if (self.messageData.direction == MsgDirectionIncoming) {
        //接收方
          [self.avatarView sd_setImageWithURL:data.friendAvatarUrl      placeholderImage:self.messageData.avatarImage];
    }else
    {
        //发送方
         [self.avatarView sd_setImageWithURL:data.userAvatarUrl placeholderImage:self.messageData.avatarImage];
    }

五.点击聊天(聊天发送消息时,自己头像的改变)

虽然聊天页面有用户头像,但是当发送消息时,又会显示默认头像,所以在发送消息时,设置自己的头像。

在发送信息时,实现TUIInputController的代理方法的地方重新赋值。

- (void)inputController:(TUIInputController *)inputController didSendMessage:(TUIMessageCellData *)msg

- (void)changeMsg:(TUIMessageCellData *)msg status:(TMsgStatus)status
{
    msg.status = status;
    NSInteger index = [_uiMsgs indexOfObject:msg];
    TUIMessageCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    msg.userAvatarUrl =[NSURL URLWithString: USERMODEL.headImg];
    [cell fillWithData:msg];
}

六.自定义消息

需求:在用户聊天界面点击+号,弹出相册,视频类的快捷发送消息。

图片描述
设置:
(1).打开TUIInputMoreCellData文件,在.h文件添加

@property (class, nonatomic, assign) TUIInputMoreCellData *shopData;
@property (class, nonatomic, assign) TUIInputMoreCellData *buyData;
@property (class, nonatomic, assign) TUIInputMoreCellData *couponData;

在.m文件添加

+ (void)setVideoData:(TUIInputMoreCellData *)videoData
{
    TUI_Video_MoreCell = videoData;
}

+ (TUIInputMoreCellData *)shopData
{
    if (!TUI_File_ShopCell) {
        TUI_File_ShopCell = [[TUIInputMoreCellData alloc] init];
        TUI_File_ShopCell.title = @"商城";
        TUI_File_ShopCell.image = [UIImage imageNamed:@"shop"];
    }
    return TUI_File_ShopCell;
}

+ (void)setShopData:(TUIInputMoreCellData *)shopData
{
    TUI_File_ShopCell = shopData;
}

+ (TUIInputMoreCellData *)buyData
{
    if (!TUI_File_BuyCell) {
        TUI_File_BuyCell = [[TUIInputMoreCellData alloc] init];
        TUI_File_BuyCell.title = @"内购";
        TUI_File_BuyCell.image = [UIImage imageNamed:@"order"];
    }
    return TUI_File_BuyCell;
}

+ (void)setBuyData:(TUIInputMoreCellData *)buyData
{
    TUI_File_BuyCell = buyData;
}

+ (TUIInputMoreCellData *)couponData
{
    if (!TUI_File_CouponCell) {
        TUI_File_CouponCell = [[TUIInputMoreCellData alloc] init];
        TUI_File_CouponCell.title = @"优惠券";
        TUI_File_CouponCell.image = [UIImage imageNamed:@"coupon"];
    }
    return TUI_File_CouponCell;
}

+ (void)setCouponData:(TUIInputMoreCellData *)couponData
{
    TUI_File_CouponCell = couponData;
}

(2).打开TUIChatController.m文件,把商城,内购,优惠券对象放进数组中。

 NSMutableArray *moreMenus = [NSMutableArray array];
        [moreMenus addObject:[TUIInputMoreCellData photoData]];
        [moreMenus addObject:[TUIInputMoreCellData pictureData]];
        [moreMenus addObject:[TUIInputMoreCellData videoData]];
        [moreMenus addObject:[TUIInputMoreCellData shopData]];
        [moreMenus addObject:[TUIInputMoreCellData buyData]];
         [moreMenus addObject:[TUIInputMoreCellData couponData]];
        _moreMenus = moreMenus;

在didSelectMoreCell方法中,实现相对应的点击事件。

//点击事件
- (void)inputController:(TUIInputController *)inputController didSelectMoreCell:(TUIInputMoreCell *)cell

(3)打开文件TUIMoreView,设置需要的格式。

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消