XYIMBaseManager.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. //
  2. // XYIMBaseManager.m
  3. // Timi
  4. //
  5. // Created by 翟玉磊 on 2021/3/25.
  6. //
  7. #import "XYIMBaseManager.h"
  8. #import <RongIMKit/RongIMKit.h>
  9. #import "XYChatRTMManager.h"
  10. #import "TMRoomJoinMessage.h"
  11. #import "TMSysNoticeMessage.h"
  12. #import "TMCustomMessage.h"
  13. #import "TMPrivateChatGiftMessage.h"
  14. #import "TMPrivateChatGameShareMessage.h"
  15. #import "TMPrivateChatFollowMessage.h"
  16. // 头条
  17. #import "XYHeadlineRoomManage.h"
  18. #pragma mark — 系统消息
  19. #import "XYVoiceMatchWaitPopupView.h"
  20. #import "XYVoiceMatchMessageModel.h"
  21. #import "XYVoiceChatManager.h"
  22. #import "XYSystemMessageViewController.h"
  23. #import "XYFriendApplyListViewController.h"
  24. #import "XYFollowMessageViewController.h"
  25. #import "XYPeiPlayCommendAlertView.h"
  26. #import "XYChatRoomDispatchManager.h"
  27. #import "XYWishGiftCompleteTipView.h"
  28. #import "XYAnniversaryManager.h"
  29. #import "XYChatRoomActivityMedalUpTipViewController.h"
  30. @interface XYIMBaseManager ()<RCIMConnectionStatusDelegate, RCIMReceiveMessageDelegate, RCIMUserInfoDataSource>
  31. @end
  32. @implementation XYIMBaseManager
  33. #pragma mark — Public
  34. - (void)im_setupAppId:(NSString *)appId {
  35. [[RCIM sharedRCIM] initWithAppKey:appId];
  36. [self configSDK];
  37. }
  38. - (void)configSDK {
  39. // 设置 RCIM 的连接状态监听器
  40. [[RCIM sharedRCIM] setConnectionStatusDelegate:self];
  41. // 设置 IMlib 的消息接收监听器
  42. [[RCIM sharedRCIM] setReceiveMessageDelegate:self];
  43. // 用户信息提供者
  44. [[RCIM sharedRCIM] setUserInfoDataSource:self];
  45. // 开启合并转发功能
  46. [[RCIM sharedRCIM] setEnableSendCombineMessage:YES];
  47. // 支持消息引用功能
  48. [RCIM sharedRCIM].enableMessageReference = YES;
  49. // 开启发送输入状态
  50. [RCIM sharedRCIM].enableTypingStatus = YES;
  51. // 关闭所有的前台消息提示音
  52. [RCIM sharedRCIM].disableMessageAlertSound = YES;
  53. // 设置不输入任何日志
  54. [RCIMClient sharedRCIMClient].logLevel = RC_Log_Level_None;
  55. // 注册自定义消息
  56. [[RCIMClient sharedRCIMClient] registerMessageType:TMRoomJoinMessage.class];
  57. [[RCIMClient sharedRCIMClient] registerMessageType:TMSysNoticeMessage.class];
  58. [[RCIMClient sharedRCIMClient] registerMessageType:TMCustomMessage.class];
  59. [[RCIMClient sharedRCIMClient] registerMessageType:TMPrivateChatGiftMessage.class];
  60. [[RCIMClient sharedRCIMClient] registerMessageType:TMPrivateChatGameShareMessage.class];
  61. [[RCIMClient sharedRCIMClient] registerMessageType:TMPrivateChatFollowMessage.class];
  62. NSLog(@"融云SDK版本:lib:%@", [[RCIMClient sharedRCIMClient] getSDKVersion]);
  63. }
  64. - (void)im_loginWithToken:(NSString *)token complete:(void(^)(BOOL success))block {
  65. [[RCIMClient sharedRCIMClient] connectWithToken:token dbOpened:^(RCDBErrorCode code) {
  66. } success:^(NSString *userId) {
  67. // 登录成功后加入头条聊天室
  68. [[XYHeadlineRoomManage shareInstance] joinRoom];
  69. [[XYIMUnReadCountManager sharedInstance] updateAllUnreadCount:nil];
  70. !block?:block(YES);
  71. NSLog(@"----------------融云IM连接成功----------------");
  72. } error:^(RCConnectErrorCode errorCode) {// token失效时重新请求业务服务端去融云创建token返回重新登录
  73. if (errorCode == RC_CONN_TOKEN_INCORRECT) {
  74. [[XYUserAPIManager new] getRtmTokenSuccessHandler:^(ZYLResponseModel *responseModel) {
  75. [[RCIMClient sharedRCIMClient] connectWithToken:[XYUserInfoManager nowUser].rtmToken dbOpened:^(RCDBErrorCode code) {
  76. } success:^(NSString *userId) {
  77. // 登录成功后加入头条聊天室
  78. [[XYHeadlineRoomManage shareInstance] joinRoom];
  79. [[XYIMUnReadCountManager sharedInstance] updateAllUnreadCount:nil];
  80. !block?:block(YES);
  81. NSLog(@"----------------融云IM连接成功----------------");
  82. } error:^(RCConnectErrorCode errorCode) {
  83. !block?:block(NO);
  84. NSString *msg = [XYIMBaseManager getErrorTextWithCode:errorCode];
  85. NSLog(@"----------------融云IM连接失败:%@----------------", msg);
  86. }];
  87. } failureHandler:^(ZYLNetworkError *error) {
  88. !block?:block(NO);
  89. NSLog(@"----------------融云IM连接失败:%@----------------", error.domain);
  90. }];
  91. }else {// errorCode != RC_CONN_TOKEN_INCORRECT
  92. !block?:block(NO);
  93. NSString *msg = [XYIMBaseManager getErrorTextWithCode:errorCode];
  94. NSLog(@"----------------融云IM连接失败:%@----------------", msg);
  95. }
  96. }];
  97. }
  98. - (void)im_logoutComplete:(void(^)(BOOL success))block {
  99. [[RCIMClient sharedRCIMClient] logout];
  100. !block?:block(YES);
  101. }
  102. /// 获取单聊会话列表
  103. - (NSArray *)getConversationList {
  104. NSArray *convs = [[RCIMClient sharedRCIMClient] getConversationList:@[@(ConversationType_PRIVATE)]];
  105. NSMutableArray *tempArray = [NSMutableArray array];
  106. for (RCConversation *conv in convs) {
  107. if (![BaseMethod isNOTNull:conv.targetId]) {
  108. continue;
  109. }
  110. [tempArray addObject:conv];
  111. }
  112. return tempArray;
  113. }
  114. /// 删除指定的单聊会话
  115. - (BOOL)removeConversation:(NSString *)conId {
  116. return [[RCIMClient sharedRCIMClient] removeConversation:ConversationType_PRIVATE targetId:conId];
  117. }
  118. /// 更新im缓存的用户信息
  119. - (void)refreshUserInfoWithUserId:(NSString *)userId name:(NSString *)name portrait:(NSString *)portrait {
  120. [[RCIM sharedRCIM] refreshUserInfoCache:[[RCUserInfo alloc] initWithUserId:userId name:StringIsEmpty(name)?@"":name portrait:StringIsEmpty(portrait)?@"":portrait] withUserId:userId];
  121. }
  122. /// 清除某个会话中的未读消息数
  123. - (BOOL)clearMessagesUnreadStatusWithConversationId:(NSString *)convId {
  124. return [[RCIMClient sharedRCIMClient] clearMessagesUnreadStatus:ConversationType_PRIVATE targetId:convId];
  125. }
  126. #pragma mark — RCIMConnectionStatusDelegate
  127. /*!
  128. IMKit连接状态的的监听器
  129. @param status SDK与融云服务器的连接状态
  130. @discussion 如果您设置了IMKit消息监听之后,当SDK与融云服务器的连接状态发生变化时,会回调此方法。
  131. */
  132. - (void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status {
  133. NSString *statusText = @"";
  134. switch (status) {
  135. case ConnectionStatus_UNKNOWN:
  136. statusText = @"未知状态";
  137. break;
  138. case ConnectionStatus_Connected:
  139. statusText = @"连接成功";
  140. break;
  141. case ConnectionStatus_NETWORK_UNAVAILABLE:
  142. statusText = @"连接过程中,当前设备网络不可用";
  143. break;
  144. case ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT:
  145. {
  146. statusText = @"当前用户在其他设备上登录,此设备被踢下线";
  147. [ApplicationDelegate logoutHandleSuccess:^(BOOL success) {
  148. }];
  149. // 防止多次出现
  150. for (CustomActionAlertController *alVC in ApplicationDelegate.tabbarVC.childViewControllers) {
  151. if ([alVC isKindOfClass:CustomActionAlertController.class]) {
  152. [alVC dismiss];
  153. }
  154. }
  155. CustomActionAlertController *alertController = [[CustomActionAlertController alloc] initWithTitle:kLocalizedString(@"下线通知") message:kLocalizedString(@"您的账号被异地登录,是否重新登录!") sure:kLocalizedString(@"重新登录") cancel:kLocalizedString(@"退出登录") selctedBlock:^(NSInteger index, NSString * _Nonnull title) {
  156. if (index == 0) {
  157. // 退出
  158. [ApplicationDelegate createQulickLoginPageVC];
  159. }
  160. if (index == 1) {
  161. // 重新登录
  162. [ApplicationDelegate createQulickLoginPageVC];
  163. }
  164. }];
  165. [alertController show];
  166. }
  167. break;
  168. case ConnectionStatus_Connecting:
  169. statusText = @"连接中";
  170. break;
  171. case ConnectionStatus_Unconnected:
  172. statusText = @"连接失败或未连接";
  173. break;
  174. case ConnectionStatus_SignOut:
  175. statusText = @"已注销";
  176. break;
  177. case ConnectionStatus_Suspend:
  178. statusText = @"连接暂时挂起(多是由于网络问题导致),SDK 会在合适时机进行自动重连";
  179. break;
  180. case ConnectionStatus_Timeout:
  181. statusText = @"自动连接超时,SDK 将不会继续连接,用户需要做超时处理,再自行调用 connectWithToken 接口进行连接";
  182. break;
  183. case ConnectionStatus_TOKEN_INCORRECT:
  184. statusText = @"Token无效";
  185. break;
  186. case ConnectionStatus_DISCONN_EXCEPTION:
  187. statusText = @"与服务器的连接已断开,用户被封禁";
  188. break;
  189. }
  190. NSLog(@"融云IM连接状态:%@", statusText);
  191. }
  192. - (void)printLog:(RCMessage *)message{
  193. #ifdef DEBUG
  194. NSDictionary *object;
  195. if ([message.content isKindOfClass:TMRoomJoinMessage.class]) {
  196. TMRoomJoinMessage *commandMessage = (TMRoomJoinMessage *)message.content;
  197. object = [commandMessage.data jsonValueDecoded];
  198. NSLog(@"RCIM入口收到的TMRoomJoinMessage数据:%@", object);
  199. }
  200. if ([message.content isKindOfClass:RCCommandMessage.class]) {
  201. RCCommandMessage *commandMessage = (RCCommandMessage *)message.content;
  202. object = [commandMessage.data jsonValueDecoded];
  203. NSLog(@"RCIM入口收到的RCCommandMessage数据:%@", object);
  204. }
  205. if ([message.content isKindOfClass:RCTextMessage.class]) {
  206. RCTextMessage *textMessage = (RCTextMessage *)message.content;
  207. object = [textMessage.extra jsonValueDecoded];
  208. NSLog(@"RCIM入口收到的RCTextMessage数据:%@", object);
  209. }
  210. if ([message.content isKindOfClass:TMSysNoticeMessage.class]) {
  211. TMSysNoticeMessage *systemMessage = (TMSysNoticeMessage *)message.content;
  212. object = [systemMessage.notice jsonValueDecoded];
  213. NSLog(@"RCIM入口收到的TMSysNoticeMessage数据:%@", object);
  214. }
  215. if ([message.content isKindOfClass:TMCustomMessage.class]) {
  216. TMCustomMessage *systemMessage = (TMCustomMessage *)message.content;
  217. NSDictionary *object = [systemMessage.data jsonValueDecoded];
  218. NSLog(@"RCIM入口收到的TMCustomMessage数据:%@", object);
  219. }
  220. #else
  221. #endif
  222. }
  223. #pragma mark — RCIMReceiveMessageDelegate
  224. /*!
  225. 接收消息的回调方法
  226. @param message 当前接收到的消息
  227. @param left 还剩余的未接收的消息数,left>=0
  228. @discussion 如果您设置了IMKit消息监听之后,SDK在接收到消息时候会执行此方法(无论App处于前台或者后台)。
  229. 其中,left为还剩余的、还未接收的消息数量。比如刚上线一口气收到多条消息时,通过此方法,您可以获取到每条消息,left会依次递减直到0。
  230. 您可以根据left数量来优化您的App体验和性能,比如收到大量消息时等待left为0再刷新UI。
  231. */
  232. - (void)onRCIMReceiveMessage:(RCMessage *)message left:(int)left {
  233. [self printLog:message];
  234. if (message.conversationType == ConversationType_PRIVATE) {
  235. if ([NSStringFromClass(message.content.class) hasPrefix:@"RC"] || [message.content isKindOfClass:TMPrivateChatGiftMessage.class] || [message.content isKindOfClass:TMPrivateChatGameShareMessage.class] || [message.content isKindOfClass:TMPrivateChatFollowMessage.class]) {
  236. // 原生消息、私聊礼物、游戏分享
  237. dispatch_async(dispatch_get_main_queue(), ^{
  238. if ([message.content isKindOfClass:RCCommandMessage.class]) {
  239. // 聊天室消息
  240. [[XYChatRTMManager sharedInstance] im_onReceived:message left:left];
  241. }else {
  242. // 私聊消息
  243. // 刷新会话列表
  244. [[NSNotificationCenter defaultCenter] postNotificationName:REFRESH_CONVERSATION_LIST_NOTIFICATION object:nil];
  245. // 更新未读消息数
  246. [[XYIMUnReadCountManager sharedInstance] updateMessageUnreadCountWithTabbarBadge:YES];
  247. if ([message.content isKindOfClass:TMPrivateChatGiftMessage.class]) {
  248. // 私聊礼物动画
  249. TMPrivateChatGiftMessage *commandMessage = (TMPrivateChatGiftMessage *)message.content;
  250. NSDictionary *object = [commandMessage.data jsonValueDecoded];
  251. [[NSNotificationCenter defaultCenter] postNotificationName:RECEIVED_PRVATE_CHAT_MESSAGE_NOTIFITION object:object];
  252. }
  253. }
  254. });
  255. }else {
  256. // 自定义的系统消息
  257. if ([message.content isKindOfClass:TMCustomMessage.class]) {
  258. // 自定义消息(系统消息、关注、召集)
  259. TMCustomMessage *systemMessage = (TMCustomMessage *)message.content;
  260. NSDictionary *object = [systemMessage.data jsonValueDecoded];
  261. dispatch_async(dispatch_get_main_queue(), ^{
  262. [self handleSystemMessageWithMessage:object message:message];
  263. });
  264. }
  265. }
  266. }else if (message.conversationType == ConversationType_CHATROOM) {
  267. dispatch_async(dispatch_get_main_queue(), ^{
  268. // 聊天室消息
  269. [[XYChatRTMManager sharedInstance] im_onReceived:message left:left];
  270. });
  271. }else {
  272. if ([message.content isKindOfClass:TMSysNoticeMessage.class]) {
  273. // 系统消息(目前没用)
  274. TMSysNoticeMessage *systemMessage = (TMSysNoticeMessage *)message.content;
  275. NSDictionary *object = [systemMessage.notice jsonValueDecoded];
  276. dispatch_async(dispatch_get_main_queue(), ^{
  277. [self handleSystemMessageWithMessage:object message:message];
  278. });
  279. }
  280. }
  281. }
  282. #pragma mark — RCIMUserInfoDataSource
  283. /*!
  284. SDK 的回调,用于向 App 获取用户信息
  285. @param userId 用户ID
  286. @param completion 获取用户信息完成之后需要执行的Block [userInfo:该用户ID对应的用户信息]
  287. @discussion SDK通过此方法获取用户信息并显示,请在completion中返回该用户ID对应的用户信息。
  288. 在您设置了用户信息提供者之后,SDK在需要显示用户信息的时候,会调用此方法,向您请求用户信息用于显示。
  289. */
  290. - (void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *userInfo))completion {
  291. [[XYUserAPIManager new] getImInfoWithUserId:userId successHandler:^(ZYLResponseModel *responseModel) {
  292. NSDictionary *userDict = responseModel.data;
  293. if (userDict) {
  294. NSString *userName = userDict[@"userName"];
  295. NSString *userAvatar = userDict[@"userAvatar"];
  296. RCUserInfo *userInfo = [[RCUserInfo alloc] initWithUserId:userId name:userName portrait:userAvatar];
  297. !completion?:completion(userInfo);
  298. }else {
  299. !completion?:completion(nil);
  300. }
  301. } failureHandler:^(ZYLNetworkError *error) {
  302. !completion?:completion(nil);
  303. }];
  304. }
  305. - (void)onReceivedHeadlineMessage:(NSNotification *)notification {
  306. NSDictionary *object = notification.object;
  307. if (object) { [self handleSystemMessageWithMessage:object message:nil];}
  308. }
  309. #pragma mark — Method
  310. + (NSString *)getErrorTextWithCode:(NSInteger)errorCode {
  311. NSString *errorMsg = @"";
  312. if (errorCode == RC_CONN_TOKEN_INCORRECT) {
  313. errorMsg = @"Token 无效";
  314. }else if (errorCode == RC_CONN_ID_REJECT) {
  315. errorMsg = @"请检查您使用的 AppKey 是否正确";
  316. }else if (errorCode == RC_CONN_NOT_AUTHRORIZED) {
  317. errorMsg = @"AppKey 与 Token 不匹配";
  318. }else if (errorCode == RC_CONN_PACKAGE_NAME_INVALID) {
  319. errorMsg = @"BundleID 不正确";
  320. }else if (errorCode == RC_CONN_APP_BLOCKED_OR_DELETED) {
  321. errorMsg = @"AppKey 被封禁或已删除";
  322. }else if (errorCode == RC_CONN_USER_BLOCKED) {
  323. errorMsg = @"用户被封禁";
  324. }else if (errorCode == RC_DISCONN_KICK) {
  325. errorMsg = @"用户被踢下线";
  326. }else if (errorCode == RC_CONN_OTHER_DEVICE_LOGIN) {
  327. errorMsg = @"用户在其它设备上登录";
  328. }else if (errorCode == CONCURRENT_LIMIT_ERROR) {
  329. errorMsg = @"连接超过并发限定值";
  330. }else if (errorCode == RC_CLIENT_NOT_INIT) {
  331. errorMsg = @"SDK 没有初始化";
  332. }else if (errorCode == RC_INVALID_PARAMETER) {
  333. errorMsg = @"开发者接口调用时传入的参数错误";
  334. }else if (errorCode == RC_CONNECTION_EXIST) {
  335. errorMsg = @"Connection 已经存在";
  336. }else if (errorCode == RC_ENVIRONMENT_ERROR) {
  337. errorMsg = @"连接环境不正确(融云公有云 SDK 无法连接到私有云环境)";
  338. }else if (errorCode == RC_CONNECT_TIMEOUT) {
  339. errorMsg = @"连接超时";
  340. }else if (errorCode == RC_INVALID_ARGUMENT) {
  341. errorMsg = @"开发者接口调用时传入的参数错误";
  342. }else if (errorCode == DATABASE_ERROR) {
  343. errorMsg = @"数据库错误";
  344. }
  345. return errorMsg;
  346. }
  347. - (void)handleSystemMessageWithMessage:(NSDictionary *)msg message:(RCMessage *)message {
  348. if (ObjectIsNil(msg)) {
  349. return;
  350. }
  351. NSString *type = msg[@"type"];
  352. if (StringIsEmpty(type)) {
  353. type = msg[@"msg"][@"type"];
  354. }
  355. if (StringIsEmpty(type)) {
  356. return;
  357. }
  358. if ([type isEqualToString:USER_KICK]) {
  359. // 用户踢下线
  360. // 防止多次出现
  361. for (CustomActionAlertController *alVC in ApplicationDelegate.tabbarVC.childViewControllers) {
  362. if ([alVC isKindOfClass:CustomActionAlertController.class]) {
  363. [alVC dismiss];
  364. }
  365. }
  366. // 退出状态
  367. [ApplicationDelegate logoutHandleSuccess:^(BOOL success) {
  368. }];
  369. CustomActionAlertController *alertController = [[CustomActionAlertController alloc] initWithTitle:kLocalizedString(@"下线通知") message:msg[@"kickReason"] sure:kLocalizedString(@"确定") cancel:nil selctedBlock:^(NSInteger index, NSString * _Nonnull title) {
  370. [ApplicationDelegate createQulickLoginPageVC];
  371. }];
  372. [alertController show];
  373. }
  374. if ([type isEqualToString:VOICE_CHAT_MATCH_WAIT]) {
  375. // 语音速配等待同意
  376. XYVoiceMatchMessageModel *model = XYVoiceMatchMessageModel.new;
  377. [model yy_modelSetWithDictionary:msg];
  378. if ([model.userId isEqualToString:[XYUserInfoManager nowUser].userId]) {
  379. // 自己发起的速配,等待对方处理就行了,自己不需要处理
  380. return;
  381. }
  382. XYVoiceMatchWaitPopupView *waitView = [XYVoiceMatchWaitPopupView sharedInstance];
  383. [waitView setupVoiceMatchWaitMessage:model];
  384. [waitView show];
  385. [waitView setVoiceMatchWaitrejectBlock:^{
  386. // 拒绝
  387. [self voiceMatchWaitDecision:0 hisId:model.hisId round:model.round];
  388. }];
  389. [waitView setVoiceMatchWaitAgreeBlock:^{
  390. // 同意
  391. [self voiceMatchWaitDecision:1 hisId:model.hisId round:model.round];
  392. }];
  393. }
  394. if ([type isEqualToString:VOICE_CHAT_MATCH_FAIL]) {
  395. // 语音速配失败
  396. XYVoiceMatchMessageModel *model = XYVoiceMatchMessageModel.new;
  397. [model yy_modelSetWithDictionary:msg];
  398. if ([model.targetUserId isEqualToString:[XYUserInfoManager nowUser].userId]) {
  399. // 自己是接收方,速配失败则取消显示的弹窗
  400. [[XYVoiceMatchWaitPopupView sharedInstance] dismiss];
  401. return;
  402. }
  403. [[NSNotificationCenter defaultCenter] postNotificationName:VOICE_MATCH_NOTIFICATION object:msg];
  404. }
  405. if ([type isEqualToString:VOICE_CHAT_MATCH_SUCCESS]) {
  406. // 语音速配成功
  407. XYVoiceMatchMessageModel *model = XYVoiceMatchMessageModel.new;
  408. [model yy_modelSetWithDictionary:msg];
  409. if ([model.targetUserId isEqualToString:[XYUserInfoManager nowUser].userId]) {
  410. // 自己是接收方,速配失败则取消显示的弹窗
  411. [[XYVoiceChatManager sharedInstance] pushVoiceChatWithHisId:model.hisId token:model.targetUserToken channelName:model.channelName block:^{
  412. }];
  413. }else {
  414. // 发起方
  415. [[NSNotificationCenter defaultCenter] postNotificationName:VOICE_MATCH_NOTIFICATION object:msg];
  416. }
  417. }
  418. if ([type isEqualToString:VOICE_CHAT_REJECT]) {
  419. // 语音速配拒绝
  420. XYVoiceMatchMessageModel *model = XYVoiceMatchMessageModel.new;
  421. [model yy_modelSetWithDictionary:msg];
  422. if ([model.targetUserId isEqualToString:[XYUserInfoManager nowUser].userId]) {
  423. // 自己是接收方,速配失败则取消显示的弹窗
  424. [[XYVoiceMatchWaitPopupView sharedInstance] dismiss];
  425. }else {
  426. // 发起方
  427. [[NSNotificationCenter defaultCenter] postNotificationName:VOICE_MATCH_NOTIFICATION object:msg];
  428. }
  429. }
  430. if ([type isEqualToString:VOICE_CHAT_START]) {
  431. // 语音速配开始
  432. [[NSNotificationCenter defaultCenter] postNotificationName:VOICE_MATCH_NOTIFICATION object:msg];
  433. }
  434. if ([type isEqualToString:VOICE_CHAT_STOP]) {
  435. // 语音速配结束
  436. [[NSNotificationCenter defaultCenter] postNotificationName:VOICE_MATCH_NOTIFICATION object:msg];
  437. }
  438. if ([type isEqualToString:VOICE_CHAT_CANCEL]) {
  439. // 语音速配取消
  440. XYVoiceMatchMessageModel *model = XYVoiceMatchMessageModel.new;
  441. [model yy_modelSetWithDictionary:msg];
  442. if ([model.targetUserId isEqualToString:[XYUserInfoManager nowUser].userId]) {
  443. // 自己是接收方,速配失败则取消显示的弹窗
  444. [[XYVoiceMatchWaitPopupView sharedInstance] dismiss];
  445. }else {
  446. // 发起方
  447. [[NSNotificationCenter defaultCenter] postNotificationName:VOICE_MATCH_NOTIFICATION object:msg];
  448. }
  449. }
  450. if ([type isEqualToString:SYSTEM_MSG]) {
  451. // 系统消息
  452. Notification *systemNotification = (Notification *)[DataBase getEntityWithName:@"Notification"];
  453. [systemNotification yy_modelSetWithDictionary:msg];
  454. systemNotification.user = [DataBase nowUser];
  455. systemNotification.messageType = SYSTEM_MSG;
  456. systemNotification.userId = [XYUserInfoManager nowUser].userId;
  457. systemNotification.msgId = [NSString stringWithFormat:@"%ld", message.messageId];
  458. systemNotification.title = [BaseMethod toString:msg[@"title"]];
  459. if ([[ApplicationDelegate getCurrentController] isKindOfClass:[XYSystemMessageViewController class]]) {
  460. systemNotification.readStatus = 0;
  461. }else {
  462. systemNotification.readStatus = 1;
  463. }
  464. systemNotification.time = [BaseMethod toString:msg[@"timestamp"]];
  465. systemNotification.content = [BaseMethod toString:msg[@"body"]];
  466. [DataBase commitDataBase];
  467. // 更新未读消息数量
  468. [[XYIMUnReadCountManager sharedInstance] updateMessageUnreadCountWithTabbarBadge:YES];
  469. // 通知会话列表更新消息数量
  470. [NotificationCenter postNotificationName:RECEIVED_SYSTEM_MESSAGE_NOTIFICATION object:nil];
  471. }
  472. // if ([type isEqualToString:FRIEND_ADD_REQUEST]) {
  473. // // 添加好友请求
  474. // NSString *sendUserId = [BaseMethod toString:msg[@"userId"]];
  475. // Notification *friendNotification = [DataBase isFriendNotification:sendUserId types:@[FRIEND_ADD_REQUEST]];
  476. // if (friendNotification) {
  477. // // 防止重复申请提示
  478. // return;
  479. // }
  480. // friendNotification = (Notification *)[DataBase getEntityWithName:@"Notification"];
  481. // friendNotification.user = [DataBase nowUser];
  482. // friendNotification.messageType = FRIEND_ADD_REQUEST;
  483. // friendNotification.userId = [BaseMethod toString:msg[@"userId"]];
  484. // friendNotification.userName = [BaseMethod toString:msg[@"userName"]];
  485. // friendNotification.msgId = [NSString stringWithFormat:@"%ld", message.messageId];;
  486. // friendNotification.title = @"添加好友";
  487. //
  488. // if ([[ApplicationDelegate getCurrentController] isKindOfClass:[XYFriendApplyListViewController class]]) {
  489. // friendNotification.readStatus = 0;
  490. // }else {
  491. // friendNotification.readStatus = 1;
  492. // }
  493. // friendNotification.time = [BaseMethod toString:msg[@"timestamp"]];
  494. // friendNotification.content = [BaseMethod toString:msg[@"content"]];
  495. //
  496. // [DataBase commitDataBase];
  497. // // 更新未读消息数量
  498. // [[XYIMUnReadCountManager sharedInstance] updateMessageUnreadCountWithTabbarBadge:YES];
  499. //
  500. // // 通知会话列表更新消息数量
  501. // [NotificationCenter postNotificationName:RECEIVED_FRIEND_MESSAGE_NOTIFITION object:nil];
  502. // }
  503. // if ([type isEqualToString:FRIEND_ADD_DENY]) {
  504. // // 拒绝添加好友
  505. // NSString *sendUserId = [BaseMethod toString:msg[@"userId"]];
  506. // Notification *friendNotification = [DataBase isFriendNotification:sendUserId types:@[FRIEND_ADD_DENY]];
  507. // if (friendNotification) {
  508. // // 防止重复申请提示
  509. // return;
  510. // }
  511. // friendNotification = (Notification *)[DataBase getEntityWithName:@"Notification"];
  512. // friendNotification.user = [DataBase nowUser];
  513. // friendNotification.messageType = FRIEND_ADD_DENY;
  514. // friendNotification.userId = [BaseMethod toString:msg[@"targetId"]];
  515. // friendNotification.userName = [BaseMethod toString:msg[@"targetName"]];
  516. // friendNotification.msgId = [NSString stringWithFormat:@"%ld", message.messageId];;
  517. // friendNotification.title = @"拒绝添加好友";
  518. //
  519. // if ([[ApplicationDelegate getCurrentController] isKindOfClass:[XYFriendApplyListViewController class]]) {
  520. // friendNotification.readStatus = 0;
  521. // }else {
  522. // friendNotification.readStatus = 1;
  523. // }
  524. // friendNotification.time = [BaseMethod toString:msg[@"timestamp"]];
  525. // friendNotification.content = [BaseMethod toString:msg[@"content"]];
  526. //
  527. // [DataBase commitDataBase];
  528. // // 更新未读消息数量
  529. // [[XYIMUnReadCountManager sharedInstance] updateMessageUnreadCountWithTabbarBadge:YES];
  530. //
  531. // // 通知会话列表更新消息数量
  532. // [NotificationCenter postNotificationName:RECEIVED_FRIEND_MESSAGE_NOTIFITION object:nil];
  533. // }
  534. // if ([type isEqualToString:FRIEND_ADD_AGREE]) {
  535. // // 同意添加好友
  536. // NSString *sendUserId = [BaseMethod toString:msg[@"userId"]];
  537. // Notification *friendNotification = [DataBase isFriendNotification:sendUserId types:@[FRIEND_ADD_AGREE]];
  538. // if (friendNotification) {
  539. // // 防止重复申请提示
  540. // return;
  541. // }
  542. // friendNotification = (Notification *)[DataBase getEntityWithName:@"Notification"];
  543. // friendNotification.user = [DataBase nowUser];
  544. // friendNotification.messageType = FRIEND_ADD_AGREE;
  545. // friendNotification.userId = [BaseMethod toString:msg[@"targetId"]];
  546. // friendNotification.userName = [BaseMethod toString:msg[@"targetName"]];
  547. // friendNotification.msgId = [NSString stringWithFormat:@"%ld", message.messageId];;
  548. // friendNotification.title = @"同意添加好友";
  549. //
  550. // if ([[ApplicationDelegate getCurrentController] isKindOfClass:[XYFriendApplyListViewController class]]) {
  551. // friendNotification.readStatus = 0;
  552. // }else {
  553. // friendNotification.readStatus = 1;
  554. // }
  555. // friendNotification.time = [BaseMethod toString:msg[@"timestamp"]];
  556. // friendNotification.content = [BaseMethod toString:msg[@"content"]];
  557. //
  558. // [DataBase commitDataBase];
  559. // // 更新未读消息数量
  560. // [[XYIMUnReadCountManager sharedInstance] updateMessageUnreadCountWithTabbarBadge:YES];
  561. //
  562. // // 通知会话列表更新消息数量
  563. // [NotificationCenter postNotificationName:RECEIVED_FRIEND_MESSAGE_NOTIFITION object:nil];
  564. // }
  565. // if ([type isEqualToString:USER_FOLLOW]) {
  566. // // 用户关注
  567. // NSString *sendUserId = [BaseMethod toString:msg[@"userId"]];
  568. // NSDictionary *userCardVO = msg[@"userCardVO"];
  569. // Notification *followNotification = [DataBase isFriendNotification:sendUserId types:@[USER_FOLLOW]];
  570. // if (followNotification == nil) {
  571. // followNotification = (Notification *)[DataBase getEntityWithName:@"Notification"];
  572. // }
  573. // [followNotification yy_modelSetWithDictionary:msg];
  574. // followNotification.user = [DataBase nowUser];
  575. // followNotification.messageType = USER_FOLLOW;
  576. // followNotification.userId = sendUserId;
  577. // followNotification.userName = userCardVO[@"userName"];
  578. // followNotification.userAvatar = userCardVO[@"userAvatar"];
  579. // followNotification.msgId = [NSString stringWithFormat:@"%ld", message.messageId];;
  580. //
  581. // followNotification.title = [BaseMethod toString:msg[@"title"]];
  582. // if ([[ApplicationDelegate getCurrentController] isKindOfClass:[XYFollowMessageViewController class]]) {
  583. // followNotification.readStatus = 0;
  584. // }else {
  585. // followNotification.readStatus = 1;
  586. // }
  587. // followNotification.time = [BaseMethod toString:msg[@"timestamp"]];
  588. // followNotification.content = [NSString stringWithFormat:@"%@ 关注了你", followNotification.userName];
  589. //
  590. // [DataBase commitDataBase];
  591. // // 更新未读消息数量
  592. // [[XYIMUnReadCountManager sharedInstance] updateMessageUnreadCountWithTabbarBadge:YES];
  593. //
  594. // // 通知会话列表更新消息数量
  595. // [NotificationCenter postNotificationName:RECEIVED_FOLLOW_MESSAGE_NOTIFITION object:nil];
  596. // }
  597. if ([type isEqualToString:USER_SCORE_CHANGE] || [type isEqualToString:RTC_SCORE_LEVEL_UP]) {
  598. //用户积分改变、用户积分升级
  599. [NotificationCenter postNotificationName:USER_INTEGRAL_CHANGE_NOTIFICATION object:msg];
  600. }
  601. if ([type isEqualToString:RTC_CONVENE_FANS_NOTICE]) {
  602. //召集粉丝
  603. /*
  604. [17:12:24] -[XYIMBaseManager handleSystemMessageWithMessage:message:] [第326行] 💕 custom_elem:{
  605. "isLock" : 0,
  606. "roomId" : 260698433126400,
  607. "seatNo" : 0,
  608. "userId" : 32609354645515,
  609. "msg" : "我正在桃桃酱房间,快来和我一起玩耍吧!!!",
  610. "roomType" : 0,
  611. "type" : "rtc:convene:fans:notice",
  612. "timestamp" : 1621416543162
  613. }
  614. */
  615. XYChatRoomListModel *roomModel = [[XYChatRoomListModel alloc] init];
  616. roomModel.roomId = msg[@"roomId"];
  617. roomModel.roomCover = msg[@"roomCover"];
  618. roomModel.roomName = msg[@"roomName"];
  619. [[XYPeiPlayCommendAlertView shareInstance] callFans:roomModel];
  620. }
  621. if ([type isEqualToString:CHAT_AUDIT_FAILED]) {
  622. //聊天内容审核失败
  623. /*
  624. "msgId" : "BQCN-FI23-AKG5-LKOK",
  625. "timestamp" : 1625206759877,
  626. "senderId" : 9294409891907,
  627. "type" : "chat:audit:failed"
  628. */
  629. [[NSNotificationCenter defaultCenter] postNotificationName:RECEIVED_PRVATE_CHAT_MESSAGE_NOTIFITION object:msg];
  630. }
  631. if ([type isEqualToString:RTC_SKILL_ORDER_CHANGE]) {//订单大厅派单数量
  632. [[XYIMUnReadCountManager sharedInstance] receiveNewDispatchOrderMessage:msg tabbarBadge:YES];
  633. [[NSNotificationCenter defaultCenter] postNotificationName:IM_RECEIVED_DISPATCH_SKILL_ORDER_MESSAGE_NOTIFICATION object:msg];
  634. }
  635. if ([type isEqualToString:RTC_SKILL_ORDER_DETAIL]) {//派单详情
  636. [[XYChatRoomDispatchManager shareInstance] reloadSkillTopAlert:msg];
  637. }
  638. #pragma mark - 许愿任务完成个人消息
  639. if ([type isEqualToString:WISH_TASK_COMPETE]) {
  640. [XYWishGiftCompleteTipView show:msg];
  641. // 心愿任务状态变更通知
  642. [[NSNotificationCenter defaultCenter] postNotificationName:WISH_TASK_SWITCH_NOTIFICATION object:nil];
  643. }
  644. #pragma mark - 首页挂件
  645. if ([type isEqualToString:APP_WIDGET_NOTIFY]) {
  646. [[XYAnniversaryManager sharedInstance] getAnniversaryConfig];
  647. }
  648. #pragma mark - 活动勋章升级消息
  649. if ([type isEqualToString:RTC_ACTIVITY_MEDAL_LEVEL_UP]) {
  650. NSString *userId = [BaseMethod toString:msg[@"msg"][@"userId"]];
  651. if ([[XYUserInfoManager nowUser].userId isEqualToString:userId]) {
  652. // 如果是自己升级则 需要弹窗提示和更新房间用户信息
  653. [XYChatRoomActivityMedalUpTipViewController showActivityMedalUpLevelWithActivityInfo:msg parentController:[ApplicationDelegate getCurrentController] completeBlock:^{
  654. }];
  655. }
  656. }
  657. }
  658. /// 语音速配-同意或拒绝
  659. /// @param decision decision 是否同意 1同意 0拒绝
  660. /// @param hisId 匹配记录id
  661. - (void)voiceMatchWaitDecision:(NSInteger)decision hisId:(NSString *)hisId round:(NSInteger)round {
  662. [[XYChatAPIManager new] decideVoiceMatchWithHisId:hisId decision:decision round:round successHandler:^(ZYLResponseModel *responseModel) {
  663. if (decision == 1) {
  664. // 同意语音
  665. // [SVProgressHUD showSuccessWithStatus:@"同意语音"];
  666. }
  667. [[XYVoiceMatchWaitPopupView sharedInstance] dismiss];
  668. } failureHandler:^(ZYLNetworkError *error) {
  669. [[XYVoiceMatchWaitPopupView sharedInstance] dismiss];
  670. [SVProgressHUD showInfoWithStatus:error.domain];
  671. }];
  672. }
  673. static XYIMBaseManager *_singleInstance = nil;
  674. + (instancetype)sharedInstance {
  675. static dispatch_once_t onceToken;
  676. dispatch_once(&onceToken, ^{
  677. if (_singleInstance == nil) {
  678. _singleInstance = [[self alloc]init];
  679. }
  680. });
  681. return _singleInstance;
  682. }
  683. + (instancetype)allocWithZone:(struct _NSZone *)zone {
  684. static dispatch_once_t onceToken;
  685. dispatch_once(&onceToken, ^{
  686. _singleInstance = [super allocWithZone:zone];
  687. });
  688. return _singleInstance;
  689. }
  690. - (id)copyWithZone:(NSZone *)zone {
  691. return _singleInstance;
  692. }
  693. - (id)mutableCopyWithZone:(NSZone *)zone {
  694. return _singleInstance;
  695. }
  696. - (instancetype)init
  697. {
  698. self = [super init];
  699. if (self) {
  700. [self _addNotification];
  701. }
  702. return self;
  703. }
  704. - (void)_addNotification {
  705. /// 接收头条消息通知(目前全平台消息使用的就是此消息)
  706. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReceivedHeadlineMessage:) name:IM_RECEIVED_HEADLINE_MESSAGE_NOTIFICATION object:nil];
  707. }
  708. @end