YOUPAILCIMNotificationManager.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. //
  2. // YOUPAILCIMNotificationManager.m
  3. // LiveChat
  4. //
  5. // Created by 张灿 on 2018/9/1.
  6. // Copyright © 2018年 caicai. All rights reserved.
  7. //
  8. #import "YOUPAILCIMNotificationManager.h"
  9. #import "YOUPAILCVideoResponseVC.h"
  10. #import "YOUPAILCBaseVideoCallVC.h"
  11. #import "YOUPAILCMessageVC.h"
  12. #import <AudioToolbox/AudioToolbox.h>
  13. #import "YOUPAILPIMNotificationModel.h"
  14. #import "YOUPAILCCallAttachment.h"
  15. #import "YOUPAIBBVideoResponseVC.h"
  16. #import "YOUPAIBBCustomSystomNotification.h"
  17. #import "YOUPAILZHomeTVModel.h"
  18. #import "YOUPAILZUpgradeIntimacyLevelWindow.h"
  19. #import "YOUPAILZRedEnvelopesWindow.h"
  20. #import "UIViewController+TFPresent.h"
  21. @interface YOUPAILCIMNotificationManager()<NIMSystemNotificationManagerDelegate,NIMChatManagerDelegate>
  22. @property(nonatomic,strong)NSMutableArray* customSysNotiArray;
  23. @property (nonatomic,strong) AVAudioPlayer *player; //播放提示音
  24. @property(nonatomic,assign)BOOL isPlaying;
  25. @end
  26. @implementation YOUPAILCIMNotificationManager
  27. + (instancetype)sharedCenter
  28. {
  29. static YOUPAILCIMNotificationManager *instance = nil;
  30. static dispatch_once_t onceToken;
  31. dispatch_once(&onceToken, ^{
  32. instance = [[YOUPAILCIMNotificationManager alloc] init];
  33. });
  34. return instance;
  35. }
  36. - (void)start
  37. {
  38. NSLog(@"Notification Center Setup");
  39. }
  40. - (instancetype)init{
  41. self = [super init];
  42. if(self) {
  43. //播放收到消息的声音
  44. NSString* path = [[NSBundle mainBundle] pathForResource:@"sound"ofType:@"bundle"];
  45. NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/message.wav",path]];
  46. // NSURL *url = [path URLForResource:@"message" withExtension:@"wav"];
  47. _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
  48. [[NIMSDK sharedSDK].chatManager addDelegate:self];
  49. [[NIMSDK sharedSDK].systemNotificationManager addDelegate:self];
  50. self.customSysNotiArray = [NSMutableArray array];
  51. self.isPlaying = NO;
  52. }
  53. return self;
  54. }
  55. - (void)dealloc{
  56. [[NIMSDK sharedSDK].chatManager removeDelegate:self];
  57. }
  58. #pragma mark - NIMChatManagerDelegate
  59. - (void)onRecvMessages:(NSArray *)messages{
  60. NIMMessage *message = messages.firstObject;
  61. [self sendLocalNotificationWithMessage:message];
  62. if (message.messageType == NIMMessageTypeCustom) {
  63. NIMCustomObject *object = (NIMCustomObject*)message.messageObject;
  64. if ([object.attachment isKindOfClass:[YOUPAILCCallAttachment class]]) {
  65. YOUPAILCCallAttachment* attachment = (YOUPAILCCallAttachment*)object.attachment;
  66. if ([[LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id integerValue] == attachment.from_uid) {//代表是自己发的
  67. } else {
  68. if (attachment.status==1) {
  69. //txtStr = @"[对方已取消]";
  70. [LCSaveData saveIsVideo:NO];
  71. }
  72. }
  73. }
  74. }
  75. if ([LCSaveData getIsVideo]) {//视频中不播放
  76. YOUPAILCBaseVideoCallVC *videoVC = (YOUPAILCBaseVideoCallVC *)[ZCUtils getCurrentVC];
  77. if (message.messageType == NIMMessageTypeCustom) { // 如果是自定义消息
  78. if ([videoVC isKindOfClass:[YOUPAILCBaseVideoCallVC class]]) {
  79. NIMCustomObject *object = (NIMCustomObject*)message.messageObject;
  80. if ([object.attachment isKindOfClass:[YOUPAILCGiftAttachment class]]) {
  81. YOUPAILCGiftAttachment* attachment = (YOUPAILCGiftAttachment*)object.attachment;
  82. YOUPAILZLiveGIftModel *giftModel = [[YOUPAILZLiveGIftModel alloc] init];
  83. giftModel.youpaipavatar = attachment.from_avatar; //赠送者头像
  84. giftModel.youpaipnickName = attachment.from_nickname; //赠送者昵称
  85. giftModel.youpaipgiftImage = attachment.gift_url; //礼物图片地址
  86. giftModel.youpaipgiftCount = attachment.gift_count; //礼物个数
  87. giftModel.youpaipuser_id = attachment.from_uid; // 发送者ID
  88. giftModel.youpaipgift_id = attachment.gift_id; // //礼物id
  89. giftModel.youpaipgift_svga = attachment.gift_svga; // //svga地址
  90. giftModel.youpaipgift_type = attachment.gift_type; //礼物类型 0普通 1 svga动画礼物
  91. giftModel.youpaipto_uid = attachment.to_uid; // 接收者ID
  92. [videoVC giftMsgForIm:giftModel];
  93. }
  94. return;
  95. }
  96. } else {
  97. if ([videoVC respondsToSelector:@selector(videoingRecMessage:)]) {
  98. [videoVC videoingRecMessage:message];
  99. }
  100. }
  101. return;
  102. }
  103. [self playMessageAudioTip:message];
  104. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  105. self.isPlaying = NO;
  106. });
  107. }
  108. - (BOOL)isPlayTipAudio:(NIMMessage*)message{
  109. if([message.from isEqualToString:[LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id]){
  110. return YES;
  111. }
  112. if([[LCTools getCurrentVC] isKindOfClass:[YOUPAILCMessageVC class]]){
  113. return YES;
  114. }else{
  115. if([[LCTools getCurrentVC] isKindOfClass:[YOUPAILCIMSessionVC class]]){
  116. YOUPAILCIMSessionVC* session = (YOUPAILCIMSessionVC*)[LCTools getCurrentVC];
  117. if([session.session.sessionId isEqualToString:message.session.sessionId]){
  118. return YES;
  119. }
  120. }
  121. }
  122. return NO;
  123. }
  124. - (void)playMessageAudioTip:(NIMMessage*)message;
  125. {
  126. BOOL needPlay = YES;
  127. BOOL needShake = YES;
  128. if ([self isPlayTipAudio:message]) {
  129. needPlay = NO;
  130. needShake = NO;
  131. }
  132. if ([LCSaveData getIMMsgVoice]==NO) {
  133. needPlay = NO;
  134. }
  135. if ([LCSaveData getIMMsgShake]==NO) {
  136. needShake = NO;
  137. }
  138. if (needShake && message.session.sessionType != NIMSessionTypeChatroom) {
  139. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
  140. }
  141. if (needPlay) {
  142. [self.player stop];
  143. // [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error:nil];
  144. //modify by leo 后台也可以播放
  145. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
  146. [self.player play];
  147. }
  148. }
  149. - (void)onReceiveCustomSystemNotification:(NIMCustomSystemNotification *)notification{
  150. NSDictionary* dictData = [NSJSONSerialization JSONObjectWithData:[notification.content dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
  151. NSLog(@"⚽️⚽️⚽️ 收到系统消息 %@",dictData);
  152. if (dictData) {
  153. [self.customSysNotiArray addObject:dictData];
  154. }
  155. if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive ) {
  156. [self youpaifhandelDictData];
  157. }
  158. }
  159. - (void)youpaifhandelDictData{
  160. for (int i = (int)self.customSysNotiArray.count-1; i>=0; i--) {
  161. NSDictionary* dictData = self.customSysNotiArray[i];
  162. if (dictData && [[dictData objectForKey:@"id"]integerValue]==11) {
  163. NSString* callTime = [[dictData objectForKey:@"data"]objectForKey:@"callTime"];
  164. if ([[LCTools getCurrentTimestamp]longLongValue]-[callTime longLongValue]>180) {
  165. continue ;
  166. }
  167. if ([LCSaveData getIsVideo]) {
  168. [ZCHUDHelper showTitle:@"正在视频中"];
  169. continue ;
  170. }else{
  171. if ([LCSaveData getWhiteVersion]) {
  172. return;
  173. }
  174. [LCHttpHelper requestWithURLString:CheckVideo parameters:@{@"room_id":[[dictData objectForKey:@"data"]objectForKey:@"roomid"]} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  175. NSDictionary* dict = (NSDictionary*)responseObject;
  176. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  177. NSLog(@"CheckVideo----%@----",dict);
  178. if (code==0) {//成功
  179. if ([LCSaveData getIsVideo]==NO) {
  180. [LCSaveData saveIsVideo:YES];
  181. [[NSNotificationCenter defaultCenter] postNotificationName:@"CallVideo" object:nil userInfo:nil];
  182. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  183. YOUPAILCVideoChatModel* videoModel = [YOUPAILCVideoChatModel mj_objectWithKeyValues:[dict objectForKey:@"data"]];
  184. YOUPAILCVideoResponseVC* chatResponse = [[YOUPAILCVideoResponseVC alloc]init];
  185. chatResponse.youpaipvideoModel = videoModel;
  186. ZCBaseNavigationVC *nav = [[ZCBaseNavigationVC alloc]initWithRootViewController:chatResponse];
  187. // [self pushVC:chatResponse];
  188. chatResponse.view.backgroundColor = HexColorFromRGBA(0x000000, 0.55);
  189. chatResponse.definesPresentationContext = YES;
  190. nav.modalPresentationStyle = UIModalPresentationOverCurrentContext&UIModalPresentationOverFullScreen;
  191. [[ZCUtils getCurrentVC] presentViewController:nav animated:YES completion:^{
  192. // chatResponse.view.backgroundColor = HexColorFromRGBA(0x000000, 0.55);
  193. }];
  194. });
  195. }
  196. }
  197. } failure:^(NSError *error) {
  198. }];
  199. }
  200. }else if (dictData && ([[dictData objectForKey:@"id"]integerValue]==12||[[dictData objectForKey:@"id"]integerValue]==13||[[dictData objectForKey:@"id"]integerValue]==14)) {//只是用来做推送 不做任何处理
  201. // NSLog(@"dictData is %@",dictData);
  202. NSDictionary* dict = dictData[@"data"];
  203. YOUPAILPIMNotificationModel *model = [YOUPAILPIMNotificationModel mj_objectWithKeyValues:dict];
  204. model.notifyType = [[dictData objectForKey:@"id"]integerValue];
  205. NSDictionary *modelDic = @{@"model":model};
  206. [[NSNotificationCenter defaultCenter]postNotificationName:@"PiaoPing" object:nil userInfo:modelDic];
  207. }else if (dictData && ([[dictData objectForKey:@"id"]integerValue]==16)) {//上电视
  208. // NSLog(@"dictData is %@",dictData);
  209. NSDictionary* dict = dictData[@"data"];
  210. YOUPAILZHomeTVModel *model = [YOUPAILZHomeTVModel mj_objectWithKeyValues:dict];
  211. NSDictionary *modelDic = @{@"model":model};
  212. [[NSNotificationCenter defaultCenter]postNotificationName:@"VQUHomeTVDataNotification" object:nil userInfo:modelDic];
  213. }else if (dictData && ([[dictData objectForKey:@"id"]integerValue]==17)) {//心动值变动通知
  214. // NSLog(@"dictData is %@",dictData);
  215. NSDictionary* dict = dictData[@"data"];
  216. NSString *from_uid = dict[@"from_uid"];
  217. NSString *to_uid = dict[@"to_uid"];
  218. NSString *score = dict[@"score"];
  219. NSString *grade = dict[@"grade"];
  220. NSString *uid = [NSString stringWithFormat:@"%@",from_uid];
  221. if ([[LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id isEqual:[NSString stringWithFormat:@"%@",from_uid]]) {
  222. uid = [NSString stringWithFormat:@"%@",to_uid];
  223. }
  224. [[YOUPAILZIntimateManager shareManager].intimateDict setObject:@{
  225. @"score":[NSString stringWithFormat:@"%@",score],
  226. @"grade":[NSString stringWithFormat:@"%@",grade],
  227. } forKey:uid];
  228. [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUIMIntimateChangeNotification" object:nil userInfo:@{@"uid":uid}];
  229. }else if (dictData && ([[dictData objectForKey:@"id"]integerValue]==18)) {//心动升级通知
  230. YOUPAILZUpgradeIntimacyLevelWindow *window = [[YOUPAILZUpgradeIntimacyLevelWindow alloc] init];
  231. window.model = [YOUPAILZUpgradeIntimacyLevelModel mj_objectWithKeyValues:dictData[@"data"]];
  232. [[LCTools getCurrentVC] TFPresentVC:window completion:nil];
  233. }else if(dictData && [[dictData objectForKey:@"id"]integerValue]==19){ /// 红包消息
  234. if (![LCSaveData getWhiteVersion]) {
  235. [self performSelector:@selector(showRedEnvelopesWindowWithModel:) withObject:[YOUPAILZRedEnvelopesModel mj_objectWithKeyValues:dictData[@"data"]] afterDelay:1.0f];
  236. }
  237. }else if(dictData && [[dictData objectForKey:@"id"]integerValue]==99){
  238. NSLog(@"随机速配成功!!!!!%@", dictData);
  239. [[NSNotificationCenter defaultCenter] postNotificationName:AUDIO_VIDEO_MATCHING object:nil userInfo:dictData];
  240. }
  241. else if(dictData && [[dictData objectForKey:@"id"]integerValue]==20){
  242. // [[NSNotificationCenter defaultCenter]postNotificationName:@"UpdateUserinfo" object:nil userInfo:nil];
  243. NSDictionary* dict = dictData[@"data"];
  244. [self youpaifrefreshDataWithIsAuth:[[dict objectForKey:@"is_auth"] integerValue]];
  245. }else if (dictData && [[dictData objectForKey:@"id"]integerValue]== BBCustomSystomNotificationTypeWithCallVideo){
  246. if ([LCSaveData getIsVideo]) {
  247. [ZCHUDHelper showTitle:@"正在视频中"];
  248. continue ;
  249. }else{
  250. if ([LCSaveData getWhiteVersion]) {
  251. [LCSaveData saveIsVideo:YES];
  252. YOUPAILCVideoChatModel *videoModel = [YOUPAILCVideoChatModel mj_objectWithKeyValues:[dictData objectForKey:@"data"]];
  253. YOUPAIBBVideoResponseVC * chatResponse = [[YOUPAIBBVideoResponseVC alloc]init];
  254. chatResponse.youpaipvideoModel = videoModel;
  255. [self pushVC:chatResponse];
  256. }
  257. }
  258. }else if (dictData && [[dictData objectForKey:@"id"]integerValue]==BBCustomSystomNotificationTypeWithCancelVideo){
  259. if ([LCSaveData getWhiteVersion]) {
  260. /// 对方已取消视频电话,发出通知
  261. [[NSNotificationCenter defaultCenter]postNotificationName:@"CancelVideo" object:nil userInfo:[dictData objectForKey:@"data"]];
  262. }
  263. }else if (dictData && [[dictData objectForKey:@"id"]integerValue]==BBCustomSystomNotificationTypeWithRefuseCallVideo){
  264. if ([LCSaveData getWhiteVersion]) {
  265. ///拒绝接听视频,发出通知
  266. [[NSNotificationCenter defaultCenter]postNotificationName:@"RefuseCallVideo" object:nil userInfo:[dictData objectForKey:@"data"]];
  267. }
  268. }else if (dictData && [[dictData objectForKey:@"id"]integerValue]==BBCustomSystomNotificationTypeWithAnswerCallVideo){
  269. if ([LCSaveData getWhiteVersion]) {
  270. ///同意接听视频,发出通知
  271. [[NSNotificationCenter defaultCenter]postNotificationName:@"AnswerCallVideo" object:nil userInfo:[dictData objectForKey:@"data"]];
  272. }
  273. }else if (dictData && [[dictData objectForKey:@"id"]integerValue]== BBCustomSystomNotificationTypeWithHangupCallVideo){
  274. if ([LCSaveData getWhiteVersion]) {
  275. ///挂断视频,发出通知
  276. [[NSNotificationCenter defaultCenter]postNotificationName:@"HangupCallVideo" object:nil userInfo:[dictData objectForKey:@"data"]];
  277. }
  278. }
  279. }
  280. [self.customSysNotiArray removeAllObjects];
  281. }
  282. - (void)youpaifrefreshDataWithIsAuth:(NSInteger)isAuth{
  283. YOUPAILCUserModel* userModel = [LCSaveModel getUserModel];
  284. userModel.youpaipuserinfo.youpaipis_auth = isAuth;
  285. [LCSaveModel saveUserModel:userModel];
  286. }
  287. - (void)showRedEnvelopesWindowWithModel:(YOUPAILZRedEnvelopesModel *)model{
  288. // YOUPAILZRedEnvelopesWindow *window = [[YOUPAILZRedEnvelopesWindow alloc] init];
  289. // window.model = model;
  290. // [[LCTools getCurrentVC] TFPresentVC:window completion:nil];
  291. }
  292. - (void)pushVC:(UIViewController*)vc{
  293. ZCBaseNavigationVC *nav = [[ZCBaseNavigationVC alloc]initWithRootViewController:vc];
  294. //modify by leo fix bug ios13
  295. // [[ZCUtils getCurrentVC].navigationController presentViewController:nav animated:YES completion:^{
  296. // }];
  297. // vc.view.backgroundColor = HexColorFromRGBA(0x000000, 0.55);
  298. nav.modalPresentationStyle = UIModalPresentationFullScreen;
  299. [[LCTools getNilPresentationController:[ZCUtils getCurrentVC]] presentViewController:nav animated:YES completion:^{
  300. // vc.view.backgroundColor = HexColorFromRGBA(0x000000, 0.55);
  301. }];
  302. }
  303. - (void)sendLocalNotificationWithMessage:(NIMMessage *)message {
  304. // if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive) {
  305. // NSLog(@"输出🍀\n%@",message.senderName);
  306. // return;
  307. // }
  308. NSLog(@"输出🍀\n%@",message.apnsPayload);
  309. NSLog(@"输出🍀\n%@",message.senderName);
  310. NSString *messageType = @"";
  311. if (message.messageType == NIMMessageTypeText) {
  312. message.apnsContent = @"收到一条信息";
  313. }else if(message.messageType == NIMMessageTypeImage){
  314. message.apnsContent = @"收到一张图片";
  315. }else if(message.messageType == NIMMessageTypeAudio){
  316. message.apnsContent = @"收到一段语音";
  317. }else if(message.messageType == NIMMessageTypeVideo){
  318. message.apnsContent = @"收到一段小视频";
  319. }
  320. switch (message.messageType) {
  321. case NIMMessageTypeText:
  322. messageType = @"收到一条信息";
  323. break;
  324. case NIMMessageTypeImage:
  325. messageType = @"收到一张图片";
  326. break;
  327. case NIMMessageTypeAudio:
  328. messageType = @"收到一段语音";
  329. break;
  330. case NIMMessageTypeVideo:
  331. messageType = @"收到一段小视频";
  332. break;
  333. case NIMMessageTypeLocation:
  334. messageType = @"收到一个位置";
  335. break;
  336. case NIMMessageTypeNotification:
  337. messageType = @"收到一个通知";
  338. break;
  339. case NIMMessageTypeFile:
  340. messageType = @"收到一个文件";
  341. break;
  342. case NIMMessageTypeTip:
  343. messageType = @"收到一个提醒";
  344. break;
  345. case NIMMessageTypeRobot:
  346. messageType = @"收到一个客服消息";
  347. break;
  348. case NIMMessageTypeCustom:
  349. messageType = @"";
  350. break;
  351. }
  352. UNUserNotificationCenter *notiCenter = [UNUserNotificationCenter currentNotificationCenter];
  353. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  354. content.badge = [NSNumber numberWithInteger: [UIApplication sharedApplication].applicationIconBadgeNumber + 1];
  355. content.title = @"花蝶";
  356. content.subtitle = @"";
  357. content.body = [NSString stringWithFormat:@"%@: %@",message.senderName, messageType];
  358. content.sound = UNNotificationSound.defaultSound;
  359. UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.5 repeats:NO];
  360. NSString *idf = [NSString stringWithFormat:@"%@-%@",messageType, @([NSDate date].timeIntervalSince1970)];
  361. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:idf content:content trigger:trigger];
  362. [notiCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  363. if (!error) {
  364. NSLog(@"输出🍀\n添加通知(%@)成功",idf);
  365. }
  366. }];
  367. }
  368. @end