美文网首页
关于融云SDK的使用(IMKit)

关于融云SDK的使用(IMKit)

作者: 程序猿小武 | 来源:发表于2016-09-09 08:44 被阅读4479次

    1.注册申请APPKEY不多说了

    2.AppDelegate相关设置

    // 初始化融云SDK
    [[RCIM sharedRCIM]initWithAppKey:@"你的APPKEY"];
    

    3.与融云交互的相关代理设置

        //用户信息提供者
        [[RCIM sharedRCIM] setUserInfoDataSource:self];
        //群组信息提供者
        [[RCIM sharedRCIM] setGroupInfoDataSource:self];
        //群名片信息提供者
        [[RCIM sharedRCIM] setGroupUserInfoDataSource:self];
        //IMKit连接状态的监听器
        [[RCIM sharedRCIM] setConnectionStatusDelegate:self];
        //IMKit消息接收的监听器
        [[RCIM sharedRCIM] setReceiveMessageDelegate:self];
        //是否关闭本地通知,默认是打开的
        [[RCIM sharedRCIM] setDisableMessageNotificaiton:NO];
    

    4.实现设置好的代理

    /**
     *  用户信息提供者
     *
     *  @param userId     <#userId description#>
     *  @param completion <#completion description#>
     */
    - (void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion
    {
            //通过代理方法获得的userid去获取详细信息()
            RCUserInfo *user = [[RCUserInfo alloc]init];
            user.userId = [@"用户id"];
            user.name = [@"用户昵称"];
            user.portraitUri = [@"头像"];
            return completion(user);
    }
    
    /**
     *  群组信息提供者
     *
     *  @param groupId    <#groupId description#>
     *  @param completion <#completion description#>
     */
    -(void)getGroupInfoWithGroupId:(NSString *)groupId completion:(void (^)(RCGroup *))completion
    {
            //通过代理方法获得的groupId去获取详细信息()  
            RCGroup * group  = [[RCGroup alloc]init];
            group.groupName = [群组昵称];
            group.groupId = [群组id];
            group.portraitUri = [头像];
            return completion(group);
      
    }
    
    /**
     *  网络状态变化。
     *
     *  @param status 网络状态。
     */
    - (void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status {
        if (status == ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT) {
            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"提示"
                                  message:@"您"
                                  @"的帐号在别的设备上登录,您被迫下线!"
                                  delegate:nil
                                  cancelButtonTitle:@"知道了"
                                  otherButtonTitles:nil, nil];
            MS_LoginFirstViewController *loginfirst = [[MS_LoginFirstViewController alloc] init];
            MS_NavigationController *nav = [[MS_NavigationController alloc] initWithRootViewController:loginfirst];
            
            UIWindow *window = [[UIApplication sharedApplication] keyWindow];
            
            window.rootViewController = nav;
       
            [alert show];
          
        } else if (status == ConnectionStatus_TOKEN_INCORRECT) {
            dispatch_async(dispatch_get_main_queue(), ^{
              
                UIAlertView *alertView =
                [[UIAlertView alloc] initWithTitle:nil
                                           message:@"已过期,请重新登录"
                                          delegate:nil
                                 cancelButtonTitle:@"确定"
                                 otherButtonTitles:nil, nil];
                
                MS_LoginFirstViewController *loginfirst = [[MS_LoginFirstViewController alloc] init];
                MS_NavigationController *nav = [[MS_NavigationController alloc] initWithRootViewController:loginfirst];
                
                UIWindow *window = [[UIApplication sharedApplication] keyWindow];
                
                window.rootViewController = nav;
                
                [alertView show];
            });
        }
    }
    
    
    /*!
     接收消息的回调方法
     
     @param message     当前接收到的消息
     @param left        还剩余的未接收的消息数,left>=0
     
     @discussion 如果您设置了IMKit消息监听之后,SDK在接收到消息时候会执行此方法(无论App处于前台或者后台)。
     其中,left为还剩余的、还未接收的消息数量。比如刚上线一口气收到多条消息时,通过此方法,您可以获取到每条消息,left会依次递减直到0。
     您可以根据left数量来优化您的App体验和性能,比如收到大量消息时等待left为0再刷新UI。
     */
    -(void)onRCIMReceiveMessage:(RCMessage *)message left:(int)left{
    
    int unreadMsgCount = [[RCIMClient sharedRCIMClient] getUnreadCount:@[
                                                                             @(ConversationType_PRIVATE),
                                                                             @(ConversationType_DISCUSSION),
                                                                             @(ConversationType_APPSERVICE),
                                                                             @(ConversationType_PUBLICSERVICE), 
                                                                             @(ConversationType_GROUP) 
                                                                             ]];
                                                                             
        NSString * unreadNum = [NSString stringWithFormat:@"%d",unreadMsgCount];
        NSDictionary * dict = @{@"unreadNum":unreadNum};
    
        [[NSNotificationCenter defaultCenter] postNotificationName:@"MessageUnreadNum" object:nil userInfo:dict];
    
        float version = [[[UIDevice currentDevice] systemVersion] floatValue];
        
        if (version >= 8.0) {
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        }
        UIApplication *app = [UIApplication sharedApplication];
        app.applicationIconBadgeNumber = unreadMsgCount;
       
        
    }
    
    //这两个直接看注释,写的非常详细
    -(BOOL)onRCIMCustomLocalNotification:(RCMessage*)message
                          withSenderName:(NSString *)senderName{
        return NO;
    }
    -(BOOL)onRCIMCustomAlertSound:(RCMessage *)message{
        return NO;
    }
    

    5单聊相关

    //MS_ChatViewController是我继承融云RCConversationViewController的一个子类
    MS_ChatViewController * chat = [[MS_ChatViewController alloc]initWithConversationType:ConversationType_PRIVATE targetId:@"对方id"];
    
        [self.navigationController pushViewController:chat animated:YES];
    

    6群组相关

    //MS_MyGroupChatViewController是我继承融云RCConversationViewController的一个子类
    MS_MyGroupChatViewController * chat = [[MS_MyGroupChatViewController alloc]initWithConversationType:ConversationType_GROUP targetId:@“群组id”];
    
        [self.navigationController pushViewController:chat animated:YES];
    

    7聊天室相关

    //MS_DoctorChatDetailController是我继承融云RCConversationViewController的一个子类
     MS_DoctorChatDetailController * doc =[[MS_DoctorChatDetailController alloc]initWithConversationType:ConversationType_CHATROOM targetId:_chatRoomID];
    
        [self.navigationController pushViewController:doc animated:YES];
    
    PS:融云的缓存可能导致你头像,昵称显示不出来,清一下
    [[RCIM sharedRCIM] clearUserInfoCache];
    

    相关文章

      网友评论

          本文标题:关于融云SDK的使用(IMKit)

          本文链接:https://www.haomeiwen.com/subject/amjeettx.html