123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- //
- // TXIMLinsenter.m
- // KulexiuForTeacher
- //
- // Created by 王智 on 2023/8/7.
- //
- #import "TXIMLinsenter.h"
- #import "KSTabBarViewController.h"
- #import "AppDelegate+AppService.h"
- #import "ClassroomService.h"
- #import "TXLiveTextMessage.h"
- @interface TXIMLinsenter ()<TUILoginListener,V2TIMConversationListener,V2TIMSimpleMsgListener,V2TIMSignalingListener,V2TIMGroupListener>
- @end
- @implementation TXIMLinsenter
- + (instancetype)shareInstance {
- static TXIMLinsenter *manager = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- manager = [[TXIMLinsenter alloc] init];
- });
- return manager;
- }
- - (void)TXIMLoginWithUserId:(NSString *)userId sig:(NSString *)userSig callback:(TXIMLoginCallback)callback {
- [TUILogin addLoginListener:self];
- [[V2TIMManager sharedInstance] addConversationListener:self];
- [[V2TIMManager sharedInstance] addSimpleMsgListener:self];
- [[V2TIMManager sharedInstance] addGroupListener:self];
- [TUILogin login:CONFIG_TXSDKAPPID userID:userId userSig:userSig succ:^{
- self.loginIMSuccess = YES;
- if (callback) {
- callback(YES, nil);
- }
- [self registerAPNSDevice];
- } fail:^(int code, NSString *msg) {
- self.loginIMSuccess = NO;
- NSLog(@"IM login failure, code:%d, desc:%@", code, msg);
- callback(NO, msg);
- }];
- }
- - (void)registerAPNSDevice {
- NSData *deviceToken = [AppDelegate shareAppDelegate].deviceToken;
- if (deviceToken) {
- V2TIMAPNSConfig *config = [[V2TIMAPNSConfig alloc] init];
- config.businessID = TXOfflinePushCertificateIDForAPNS;
- config.token = deviceToken;
- [[V2TIMManager sharedInstance] setAPNS:config succ:^{
- NSLog(@"%s, succ", __func__);
- } fail:^(int code, NSString *desc) {
- NSLog(@"%s, fail, %d, %@", __func__, code, desc);
- }];
- }
- }
- - (void)getUnReadCountCallback:(UnreadCountCallback)callback {
- [[V2TIMManager sharedInstance] getTotalUnreadMessageCount:^(UInt64 totalUnreadCount) {
- callback(totalUnreadCount);
- } fail:^(int code, NSString *desc) {
- callback(0);
- }];
- }
- - (void)logoutTXIM {
- if (self.loginIMSuccess) {
- [TUILogin logout:^{
- self.loginIMSuccess = NO;
-
- } fail:^(int code, NSString *msg) {
- NSLog(@" logout failer ----- failure, code:%d, desc:%@", code, msg);
- }];
- }
- }
- - (BOOL)isCurrentUserLoginIM {
- return [TUILogin isUserLogined];
- }
- #pragma mark ----- TUILoginListener
- - (void)onConnecting {
- NSLog(@"----tx IM SDK 连接中");
- }
- - (void)onConnectSuccess {
- NSLog(@"----- tx IM SDK 连接成功");
- self.isIMConnected = YES;
- }
- - (void)onConnectFailed:(int)code err:(NSString *)err {
- NSLog(@"----- tx IM SDK 连接失败 %d 错误信息 %@", code, err);
- self.isIMConnected = NO;
- }
- - (void)onKickedOffline {
- // 被踢下线
- NSLog(@"----- 被踢下线");
- [[NSNotificationCenter defaultCenter] postNotificationName:@"otherLogin" object:nil];
- }
- - (void)onUserSigExpired {
- // 在线时票据过期
- NSLog(@"----- 票据过期");
- [[NSNotificationCenter defaultCenter] postNotificationName:@"otherLogin" object:nil];
- }
- #pragma mark ----
- - (void)onTotalUnreadMessageCountChanged:(UInt64)totalUnreadCount {
-
- NSDictionary *receiveMsg = @{@"unreadCount":@(totalUnreadCount)};
-
- [UIApplication sharedApplication].applicationIconBadgeNumber = totalUnreadCount;
- if (totalUnreadCount >= 1) {
- [[AppDelegate shareAppDelegate].tabBarController noteNewsWithIndex:2 count:totalUnreadCount];
- } else {
- [[AppDelegate shareAppDelegate].tabBarController clearNewsWithIndex:2];
- }
- [[NSNotificationCenter defaultCenter] postNotificationName:CHATVIEW_REFRESHSTATUS object:receiveMsg];
- }
- #pragma mark ------ V2TIMSimpleMsgListener
- - (void)onRecvGroupTextMessage:(NSString *)msgID groupID:(NSString *)groupID sender:(V2TIMGroupMemberInfo *)info text:(NSString *)text {
- // 收到群组文本消息
- if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
- TXLiveTextMessage *message = [[TXLiveTextMessage alloc] init];
- message.messageId = msgID;
- message.groupId = groupID;
- [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveMessageNotification object:@{@"message":message}];
- }
- }
- - (void)onRecvGroupCustomMessage:(NSString *)msgID groupID:(NSString *)groupID sender:(V2TIMGroupMemberInfo *)info customData:(NSData *)data {
- // 收到自定义群组消息
- if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
- [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveMessageNotification object:@{@"message":[data mj_JSONObject],@"msgID" : msgID, @"groupID" : groupID}];
- }
- else {
- NSDictionary *receiveMsg = @{@"message":[data mj_JSONObject],@"msgID" : msgID, @"groupID" : groupID};
- [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXMessageNotification object:receiveMsg];
- if ([[ClassroomService sharedService] isHoldTXClassroomMessage:receiveMsg]) {
- }
- }
- }
- #pragma mark ----- group listener
- - (void)onGroupAttributeChanged:(NSString *)groupID attributes:(NSMutableDictionary<NSString *,NSString *> *)attributes {
- // 收到自定义群组消息
- if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
- [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveGroupNotification object:@{@"message":attributes, @"groupID" : groupID}];
- }
- else {
- [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXClassroomGroupNotification object:@{@"message":attributes, @"groupID" : groupID}];
- }
- }
- - (void)onGroupCounterChanged:(NSString *)groupID key:(NSString *)key newValue:(NSInteger)newValue {
- // 收到自定义群组消息
- if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
- [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveGroupCountNotification object:@{@"message":@{key:@(newValue)}, @"groupID" : groupID}];
- }
- }
- @end
|