TXIMLinsenter.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. @interface TXIMLinsenter ()<TUILoginListener,V2TIMConversationListener,V2TIMSimpleMsgListener,V2TIMSignalingListener,V2TIMGroupListener>
  13. @end
  14. @implementation TXIMLinsenter
  15. + (instancetype)shareInstance {
  16. static TXIMLinsenter *manager = nil;
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. manager = [[TXIMLinsenter alloc] init];
  20. });
  21. return manager;
  22. }
  23. - (void)TXIMLoginWithUserId:(NSString *)userId sig:(NSString *)userSig callback:(TXIMLoginCallback)callback {
  24. [TUILogin addLoginListener:self];
  25. [[V2TIMManager sharedInstance] addConversationListener:self];
  26. [[V2TIMManager sharedInstance] addSimpleMsgListener:self];
  27. [[V2TIMManager sharedInstance] addGroupListener:self];
  28. [TUILogin login:CONFIG_TXSDKAPPID userID:userId userSig:userSig succ:^{
  29. self.loginIMSuccess = YES;
  30. if (callback) {
  31. callback(YES, nil);
  32. }
  33. [self registerAPNSDevice];
  34. } fail:^(int code, NSString *msg) {
  35. self.loginIMSuccess = NO;
  36. NSLog(@"IM login failure, code:%d, desc:%@", code, msg);
  37. callback(NO, msg);
  38. }];
  39. }
  40. - (void)registerAPNSDevice {
  41. NSData *deviceToken = [AppDelegate shareAppDelegate].deviceToken;
  42. if (deviceToken) {
  43. V2TIMAPNSConfig *config = [[V2TIMAPNSConfig alloc] init];
  44. config.businessID = TXOfflinePushCertificateIDForAPNS;
  45. config.token = deviceToken;
  46. [[V2TIMManager sharedInstance] setAPNS:config succ:^{
  47. NSLog(@"%s, succ", __func__);
  48. } fail:^(int code, NSString *desc) {
  49. NSLog(@"%s, fail, %d, %@", __func__, code, desc);
  50. }];
  51. }
  52. }
  53. - (void)getUnReadCountCallback:(UnreadCountCallback)callback {
  54. [[V2TIMManager sharedInstance] getTotalUnreadMessageCount:^(UInt64 totalUnreadCount) {
  55. callback(totalUnreadCount);
  56. } fail:^(int code, NSString *desc) {
  57. callback(0);
  58. }];
  59. }
  60. - (void)logoutTXIM {
  61. if (self.loginIMSuccess) {
  62. [TUILogin logout:^{
  63. self.loginIMSuccess = NO;
  64. } fail:^(int code, NSString *msg) {
  65. NSLog(@" logout failer ----- failure, code:%d, desc:%@", code, msg);
  66. }];
  67. }
  68. }
  69. - (BOOL)isCurrentUserLoginIM {
  70. return [TUILogin isUserLogined];
  71. }
  72. #pragma mark ----- TUILoginListener
  73. - (void)onConnecting {
  74. NSLog(@"----tx IM SDK 连接中");
  75. }
  76. - (void)onConnectSuccess {
  77. NSLog(@"----- tx IM SDK 连接成功");
  78. self.isIMConnected = YES;
  79. [[NSNotificationCenter defaultCenter] postNotificationName:@"TXIMConnected" object:nil];
  80. }
  81. - (void)onConnectFailed:(int)code err:(NSString *)err {
  82. NSLog(@"----- tx IM SDK 连接失败 %d 错误信息 %@", code, err);
  83. self.isIMConnected = NO;
  84. [[NSNotificationCenter defaultCenter] postNotificationName:@"networkError" object:nil];
  85. }
  86. - (void)onKickedOffline {
  87. // 被踢下线
  88. NSLog(@"----- 被踢下线");
  89. #ifdef DEBUG
  90. #else
  91. [[NSNotificationCenter defaultCenter] postNotificationName:@"otherLogin" object:nil];
  92. #endif
  93. }
  94. - (void)onUserSigExpired {
  95. // 在线时票据过期
  96. NSLog(@"----- 票据过期");
  97. #ifdef DEBUG
  98. #else
  99. [[NSNotificationCenter defaultCenter] postNotificationName:@"otherLogin" object:nil];
  100. #endif
  101. }
  102. #pragma mark ----
  103. - (void)onTotalUnreadMessageCountChanged:(UInt64)totalUnreadCount {
  104. NSDictionary *receiveMsg = @{@"unreadCount":@(totalUnreadCount)};
  105. [UIApplication sharedApplication].applicationIconBadgeNumber = totalUnreadCount;
  106. if (totalUnreadCount >= 1) {
  107. [[AppDelegate shareAppDelegate] noteIMNewsCount:totalUnreadCount];
  108. } else {
  109. [[AppDelegate shareAppDelegate] clearIMNewsCount];
  110. }
  111. [[NSNotificationCenter defaultCenter] postNotificationName:CHATVIEW_REFRESHSTATUS object:receiveMsg];
  112. }
  113. #pragma mark ------ V2TIMSimpleMsgListener
  114. - (void)onRecvGroupTextMessage:(NSString *)msgID groupID:(NSString *)groupID sender:(V2TIMGroupMemberInfo *)info text:(NSString *)text {
  115. // 收到群组文本消息
  116. if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
  117. TXLiveTextMessage *message = [[TXLiveTextMessage alloc] init];
  118. message.messageId = msgID;
  119. message.groupId = groupID;
  120. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveMessageNotification object:@{@"message":message}];
  121. }
  122. }
  123. - (void)onRecvGroupCustomMessage:(NSString *)msgID groupID:(NSString *)groupID sender:(V2TIMGroupMemberInfo *)info customData:(NSData *)data {
  124. // 收到自定义群组消息
  125. if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
  126. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveMessageNotification object:@{@"message":[data mj_JSONObject],@"msgID" : msgID, @"groupID" : groupID}];
  127. }
  128. else {
  129. NSDictionary *receiveMsg = @{@"message":[data mj_JSONObject],@"msgID" : msgID, @"groupID" : groupID};
  130. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXMessageNotification object:receiveMsg];
  131. if ([[ClassroomService sharedService] isHoldTXClassroomMessage:receiveMsg]) {
  132. }
  133. }
  134. }
  135. #pragma mark ----- group listener
  136. - (void)onGroupAttributeChanged:(NSString *)groupID attributes:(NSMutableDictionary<NSString *,NSString *> *)attributes {
  137. // 收到自定义群组消息
  138. if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
  139. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveGroupNotification object:@{@"message":attributes, @"groupID" : groupID}];
  140. }
  141. else {
  142. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXClassroomGroupNotification object:@{@"message":attributes, @"groupID" : groupID}];
  143. }
  144. }
  145. - (void)onGroupCounterChanged:(NSString *)groupID key:(NSString *)key newValue:(NSInteger)newValue {
  146. // 收到自定义群组消息
  147. if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
  148. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveGroupCountNotification object:@{@"message":@{key:@(newValue)}, @"groupID" : groupID}];
  149. }
  150. }
  151. @end