TXIMLinsenter.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // TXIMLinsenter.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2023/8/7.
  6. //
  7. #import "TXIMLinsenter.h"
  8. #import "KSTabBarViewController.h"
  9. #import "AppDelegate+AppService.h"
  10. #import "ClassroomService.h"
  11. #import "TXLiveTextMessage.h"
  12. #import "KSLogManager.h"
  13. @interface TXIMLinsenter ()<TUILoginListener,V2TIMConversationListener,V2TIMSimpleMsgListener,V2TIMSignalingListener,V2TIMGroupListener>
  14. @end
  15. @implementation TXIMLinsenter
  16. + (instancetype)shareInstance {
  17. static TXIMLinsenter *manager = nil;
  18. static dispatch_once_t onceToken;
  19. dispatch_once(&onceToken, ^{
  20. manager = [[TXIMLinsenter alloc] init];
  21. });
  22. return manager;
  23. }
  24. - (void)TXIMLoginWithUserId:(NSString *)userId sig:(NSString *)userSig callback:(TXIMLoginCallback)callback {
  25. [TUILogin addLoginListener:self];
  26. [[V2TIMManager sharedInstance] addConversationListener:self];
  27. [[V2TIMManager sharedInstance] addSimpleMsgListener:self];
  28. [[V2TIMManager sharedInstance] addGroupListener:self];
  29. [TUILogin login:CONFIG_TXSDKAPPID userID:userId userSig:userSig succ:^{
  30. self.loginIMSuccess = YES;
  31. if (callback) {
  32. callback(YES, nil);
  33. }
  34. [self registerAPNSDevice];
  35. } fail:^(int code, NSString *msg) {
  36. self.loginIMSuccess = NO;
  37. NSLog(@"IM login failure, code:%d, desc:%@", code, msg);
  38. callback(NO, msg);
  39. }];
  40. }
  41. - (void)registerAPNSDevice {
  42. NSData *deviceToken = [AppDelegate shareAppDelegate].deviceToken;
  43. if (deviceToken) {
  44. V2TIMAPNSConfig *config = [[V2TIMAPNSConfig alloc] init];
  45. config.businessID = TXOfflinePushCertificateIDForAPNS;
  46. config.token = deviceToken;
  47. [[V2TIMManager sharedInstance] setAPNS:config succ:^{
  48. NSLog(@"%s, succ", __func__);
  49. } fail:^(int code, NSString *desc) {
  50. NSLog(@"%s, fail, %d, %@", __func__, code, desc);
  51. }];
  52. }
  53. }
  54. - (void)getUnReadCountCallback:(UnreadCountCallback)callback {
  55. [[V2TIMManager sharedInstance] getTotalUnreadMessageCount:^(UInt64 totalUnreadCount) {
  56. callback(totalUnreadCount);
  57. } fail:^(int code, NSString *desc) {
  58. callback(0);
  59. }];
  60. }
  61. - (void)logoutTXIM {
  62. if (self.loginIMSuccess) {
  63. [TUILogin logout:^{
  64. self.loginIMSuccess = NO;
  65. } fail:^(int code, NSString *msg) {
  66. NSLog(@" logout failer ----- failure, code:%d, desc:%@", code, msg);
  67. }];
  68. }
  69. }
  70. - (BOOL)isCurrentUserLoginIM {
  71. return [TUILogin isUserLogined];
  72. }
  73. #pragma mark ----- TUILoginListener
  74. - (void)onConnecting {
  75. NSLog(@"----tx IM SDK 连接中");
  76. }
  77. - (void)onConnectSuccess {
  78. NSLog(@"----- tx IM SDK 连接成功");
  79. self.isIMConnected = YES;
  80. [[NSNotificationCenter defaultCenter] postNotificationName:@"TXIMConnected" object:nil];
  81. #pragma mark ----- IM连接成功埋点
  82. NSDictionary *parm = @{
  83. @"userId" :UserDefault(UIDKey),
  84. @"Location" : @"IM Connected",
  85. @"version" : [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"],
  86. };
  87. NSString *content = [parm mj_JSONString];
  88. NSMutableDictionary *uploadParm = [KSLogManager generateLogMessageWithContent:content type:@"RECORD"];
  89. NSMutableArray *uploadArray = [NSMutableArray arrayWithObject:uploadParm];
  90. [KSNetworkingManager sysExceptionLogUpdate:KS_POST token:self.access_token logArray:uploadArray success:^(NSDictionary * _Nonnull dic) {
  91. if ([dic ks_integerValueForKey:@"code"] == 200) {
  92. }
  93. } faliure:^(NSError * _Nonnull error) {
  94. }];
  95. }
  96. - (void)onConnectFailed:(int)code err:(NSString *)err {
  97. NSLog(@"----- tx IM SDK 连接失败 %d 错误信息 %@", code, err);
  98. self.isIMConnected = NO;
  99. [[NSNotificationCenter defaultCenter] postNotificationName:@"networkError" object:nil];
  100. }
  101. - (void)onKickedOffline {
  102. // 被踢下线
  103. NSLog(@"----- 被踢下线");
  104. //#ifdef DEBUG
  105. //
  106. //#else
  107. [[NSNotificationCenter defaultCenter] postNotificationName:@"otherLogin" object:nil];
  108. //#endif
  109. }
  110. - (void)onUserSigExpired {
  111. // 在线时票据过期
  112. NSLog(@"----- 票据过期");
  113. #ifdef DEBUG
  114. #else
  115. [[NSNotificationCenter defaultCenter] postNotificationName:@"otherLogin" object:nil];
  116. #endif
  117. }
  118. #pragma mark ----
  119. - (void)onTotalUnreadMessageCountChanged:(UInt64)totalUnreadCount {
  120. NSDictionary *receiveMsg = @{@"unreadCount":@(totalUnreadCount)};
  121. [UIApplication sharedApplication].applicationIconBadgeNumber = totalUnreadCount;
  122. if (totalUnreadCount >= 1) {
  123. [[AppDelegate shareAppDelegate].tabBarController noteNewsWithIndex:2 count:totalUnreadCount];
  124. } else {
  125. [[AppDelegate shareAppDelegate].tabBarController clearNewsWithIndex:2];
  126. }
  127. [[NSNotificationCenter defaultCenter] postNotificationName:CHATVIEW_REFRESHSTATUS object:receiveMsg];
  128. }
  129. #pragma mark ------ V2TIMSimpleMsgListener
  130. - (void)onRecvGroupTextMessage:(NSString *)msgID groupID:(NSString *)groupID sender:(V2TIMGroupMemberInfo *)info text:(NSString *)text {
  131. // 收到群组文本消息
  132. if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
  133. TXLiveTextMessage *message = [[TXLiveTextMessage alloc] init];
  134. message.messageId = msgID;
  135. message.groupId = groupID;
  136. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveMessageNotification object:@{@"message":message}];
  137. }
  138. }
  139. - (void)onRecvGroupCustomMessage:(NSString *)msgID groupID:(NSString *)groupID sender:(V2TIMGroupMemberInfo *)info customData:(NSData *)data {
  140. // 收到自定义群组消息
  141. if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
  142. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveMessageNotification object:@{@"message":[data mj_JSONObject],@"msgID" : msgID, @"groupID" : groupID}];
  143. }
  144. else {
  145. NSDictionary *receiveMsg = @{@"message":[data mj_JSONObject],@"msgID" : msgID, @"groupID" : groupID};
  146. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXMessageNotification object:receiveMsg];
  147. if ([[ClassroomService sharedService] isHoldTXClassroomMessage:receiveMsg]) {
  148. }
  149. }
  150. }
  151. #pragma mark ----- group listener
  152. - (void)onGroupAttributeChanged:(NSString *)groupID attributes:(NSMutableDictionary<NSString *,NSString *> *)attributes {
  153. // 收到自定义群组消息
  154. if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
  155. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveGroupNotification object:@{@"message":attributes, @"groupID" : groupID}];
  156. }
  157. else {
  158. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXClassroomGroupNotification object:@{@"message":attributes, @"groupID" : groupID}];
  159. }
  160. }
  161. - (void)onGroupCounterChanged:(NSString *)groupID key:(NSString *)key newValue:(NSInteger)newValue {
  162. // 收到自定义群组消息
  163. if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
  164. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveGroupCountNotification object:@{@"message":@{key:@(newValue)}, @"groupID" : groupID}];
  165. }
  166. }
  167. @end