TXIMLinsenter.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. }
  80. - (void)onConnectFailed:(int)code err:(NSString *)err {
  81. NSLog(@"----- tx IM SDK 连接失败 %d 错误信息 %@", code, err);
  82. self.isIMConnected = NO;
  83. }
  84. - (void)onKickedOffline {
  85. // 被踢下线
  86. NSLog(@"----- 被踢下线");
  87. [[NSNotificationCenter defaultCenter] postNotificationName:@"otherLogin" object:nil];
  88. }
  89. - (void)onUserSigExpired {
  90. // 在线时票据过期
  91. NSLog(@"----- 票据过期");
  92. [[NSNotificationCenter defaultCenter] postNotificationName:@"otherLogin" object:nil];
  93. }
  94. #pragma mark ----
  95. - (void)onTotalUnreadMessageCountChanged:(UInt64)totalUnreadCount {
  96. NSDictionary *receiveMsg = @{@"unreadCount":@(totalUnreadCount)};
  97. [UIApplication sharedApplication].applicationIconBadgeNumber = totalUnreadCount;
  98. if (totalUnreadCount >= 1) {
  99. [[AppDelegate shareAppDelegate].tabBarController noteNewsWithIndex:2 count:totalUnreadCount];
  100. } else {
  101. [[AppDelegate shareAppDelegate].tabBarController clearNewsWithIndex:2];
  102. }
  103. [[NSNotificationCenter defaultCenter] postNotificationName:CHATVIEW_REFRESHSTATUS object:receiveMsg];
  104. }
  105. #pragma mark ------ V2TIMSimpleMsgListener
  106. - (void)onRecvGroupTextMessage:(NSString *)msgID groupID:(NSString *)groupID sender:(V2TIMGroupMemberInfo *)info text:(NSString *)text {
  107. // 收到群组文本消息
  108. if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
  109. TXLiveTextMessage *message = [[TXLiveTextMessage alloc] init];
  110. message.messageId = msgID;
  111. message.groupId = groupID;
  112. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveMessageNotification object:@{@"message":message}];
  113. }
  114. }
  115. - (void)onRecvGroupCustomMessage:(NSString *)msgID groupID:(NSString *)groupID sender:(V2TIMGroupMemberInfo *)info customData:(NSData *)data {
  116. // 收到自定义群组消息
  117. if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
  118. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveMessageNotification object:@{@"message":[data mj_JSONObject],@"msgID" : msgID, @"groupID" : groupID}];
  119. }
  120. else {
  121. NSDictionary *receiveMsg = @{@"message":[data mj_JSONObject],@"msgID" : msgID, @"groupID" : groupID};
  122. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXMessageNotification object:receiveMsg];
  123. if ([[ClassroomService sharedService] isHoldTXClassroomMessage:receiveMsg]) {
  124. }
  125. }
  126. }
  127. #pragma mark ----- group listener
  128. - (void)onGroupAttributeChanged:(NSString *)groupID attributes:(NSMutableDictionary<NSString *,NSString *> *)attributes {
  129. // 收到自定义群组消息
  130. if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
  131. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveGroupNotification object:@{@"message":attributes, @"groupID" : groupID}];
  132. }
  133. else {
  134. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXClassroomGroupNotification object:@{@"message":attributes, @"groupID" : groupID}];
  135. }
  136. }
  137. - (void)onGroupCounterChanged:(NSString *)groupID key:(NSString *)key newValue:(NSInteger)newValue {
  138. // 收到自定义群组消息
  139. if ([groupID containsString:@"LIVE"]) { // 判断是否是直播群消息
  140. [[NSNotificationCenter defaultCenter] postNotificationName:OnReceiveTXLiveGroupCountNotification object:@{@"message":@{key:@(newValue)}, @"groupID" : groupID}];
  141. }
  142. }
  143. @end