// // YOUPAILCIMNotificationManager.m // LiveChat // // Created by 张灿 on 2018/9/1. // Copyright © 2018年 caicai. All rights reserved. // #import "YOUPAILCIMNotificationManager.h" #import "YOUPAILCVideoResponseVC.h" #import "YOUPAILCBaseVideoCallVC.h" #import "YOUPAILCMessageVC.h" #import #import "YOUPAILPIMNotificationModel.h" #import "YOUPAILCCallAttachment.h" #import "YOUPAIBBVideoResponseVC.h" #import "YOUPAIBBCustomSystomNotification.h" #import "YOUPAILZHomeTVModel.h" #import "YOUPAILZUpgradeIntimacyLevelWindow.h" #import "YOUPAILZRedEnvelopesWindow.h" #import "UIViewController+TFPresent.h" @interface YOUPAILCIMNotificationManager() @property(nonatomic,strong)NSMutableArray* customSysNotiArray; @property (nonatomic,strong) AVAudioPlayer *player; //播放提示音 @property(nonatomic,assign)BOOL isPlaying; @end @implementation YOUPAILCIMNotificationManager + (instancetype)sharedCenter { static YOUPAILCIMNotificationManager *instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[YOUPAILCIMNotificationManager alloc] init]; }); return instance; } - (void)start { NSLog(@"Notification Center Setup"); } - (instancetype)init{ self = [super init]; if(self) { //播放收到消息的声音 NSString* path = [[NSBundle mainBundle] pathForResource:@"sound"ofType:@"bundle"]; NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/message.wav",path]]; // NSURL *url = [path URLForResource:@"message" withExtension:@"wav"]; _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; [[NIMSDK sharedSDK].chatManager addDelegate:self]; [[NIMSDK sharedSDK].systemNotificationManager addDelegate:self]; self.customSysNotiArray = [NSMutableArray array]; self.isPlaying = NO; } return self; } - (void)dealloc{ [[NIMSDK sharedSDK].chatManager removeDelegate:self]; } #pragma mark - NIMChatManagerDelegate - (void)onRecvMessages:(NSArray *)messages{ NIMMessage *message = messages.firstObject; [self sendLocalNotificationWithMessage:message]; if (message.messageType == NIMMessageTypeCustom) { NIMCustomObject *object = (NIMCustomObject*)message.messageObject; if ([object.attachment isKindOfClass:[YOUPAILCCallAttachment class]]) { YOUPAILCCallAttachment* attachment = (YOUPAILCCallAttachment*)object.attachment; if ([[LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id integerValue] == attachment.from_uid) {//代表是自己发的 } else { if (attachment.status==1) { //txtStr = @"[对方已取消]"; [LCSaveData saveIsVideo:NO]; } } } } if ([LCSaveData getIsVideo]) {//视频中不播放 YOUPAILCBaseVideoCallVC *videoVC = (YOUPAILCBaseVideoCallVC *)[ZCUtils getCurrentVC]; if (message.messageType == NIMMessageTypeCustom) { // 如果是自定义消息 if ([videoVC isKindOfClass:[YOUPAILCBaseVideoCallVC class]]) { NIMCustomObject *object = (NIMCustomObject*)message.messageObject; if ([object.attachment isKindOfClass:[YOUPAILCGiftAttachment class]]) { YOUPAILCGiftAttachment* attachment = (YOUPAILCGiftAttachment*)object.attachment; YOUPAILZLiveGIftModel *giftModel = [[YOUPAILZLiveGIftModel alloc] init]; giftModel.youpaipavatar = attachment.from_avatar; //赠送者头像 giftModel.youpaipnickName = attachment.from_nickname; //赠送者昵称 giftModel.youpaipgiftImage = attachment.gift_url; //礼物图片地址 giftModel.youpaipgiftCount = attachment.gift_count; //礼物个数 giftModel.youpaipuser_id = attachment.from_uid; // 发送者ID giftModel.youpaipgift_id = attachment.gift_id; // //礼物id giftModel.youpaipgift_svga = attachment.gift_svga; // //svga地址 giftModel.youpaipgift_type = attachment.gift_type; //礼物类型 0普通 1 svga动画礼物 giftModel.youpaipto_uid = attachment.to_uid; // 接收者ID [videoVC giftMsgForIm:giftModel]; } return; } } else { if ([videoVC respondsToSelector:@selector(videoingRecMessage:)]) { [videoVC videoingRecMessage:message]; } } return; } [self playMessageAudioTip:message]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.isPlaying = NO; }); } - (BOOL)isPlayTipAudio:(NIMMessage*)message{ if([message.from isEqualToString:[LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id]){ return YES; } if([[LCTools getCurrentVC] isKindOfClass:[YOUPAILCMessageVC class]]){ return YES; }else{ if([[LCTools getCurrentVC] isKindOfClass:[YOUPAILCIMSessionVC class]]){ YOUPAILCIMSessionVC* session = (YOUPAILCIMSessionVC*)[LCTools getCurrentVC]; if([session.session.sessionId isEqualToString:message.session.sessionId]){ return YES; } } } return NO; } - (void)playMessageAudioTip:(NIMMessage*)message; { BOOL needPlay = YES; BOOL needShake = YES; if ([self isPlayTipAudio:message]) { needPlay = NO; needShake = NO; } if ([LCSaveData getIMMsgVoice]==NO) { needPlay = NO; } if ([LCSaveData getIMMsgShake]==NO) { needShake = NO; } if (needShake && message.session.sessionType != NIMSessionTypeChatroom) { AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); } if (needPlay) { [self.player stop]; // [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error:nil]; //modify by leo 后台也可以播放 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; [self.player play]; } } - (void)onReceiveCustomSystemNotification:(NIMCustomSystemNotification *)notification{ NSDictionary* dictData = [NSJSONSerialization JSONObjectWithData:[notification.content dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil]; NSLog(@"⚽️⚽️⚽️ 收到系统消息 %@",dictData); if (dictData) { [self.customSysNotiArray addObject:dictData]; } if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive ) { [self youpaifhandelDictData]; } } - (void)youpaifhandelDictData{ for (int i = (int)self.customSysNotiArray.count-1; i>=0; i--) { NSDictionary* dictData = self.customSysNotiArray[i]; if (dictData && [[dictData objectForKey:@"id"]integerValue]==11) { NSString* callTime = [[dictData objectForKey:@"data"]objectForKey:@"callTime"]; if ([[LCTools getCurrentTimestamp]longLongValue]-[callTime longLongValue]>180) { continue ; } if ([LCSaveData getIsVideo]) { [ZCHUDHelper showTitle:@"正在视频中"]; continue ; }else{ if ([LCSaveData getWhiteVersion]) { return; } [LCHttpHelper requestWithURLString:CheckVideo parameters:@{@"room_id":[[dictData objectForKey:@"data"]objectForKey:@"roomid"]} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; NSLog(@"CheckVideo----%@----",dict); if (code==0) {//成功 if ([LCSaveData getIsVideo]==NO) { [LCSaveData saveIsVideo:YES]; [[NSNotificationCenter defaultCenter] postNotificationName:@"CallVideo" object:nil userInfo:nil]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ YOUPAILCVideoChatModel* videoModel = [YOUPAILCVideoChatModel mj_objectWithKeyValues:[dict objectForKey:@"data"]]; YOUPAILCVideoResponseVC* chatResponse = [[YOUPAILCVideoResponseVC alloc]init]; chatResponse.youpaipvideoModel = videoModel; ZCBaseNavigationVC *nav = [[ZCBaseNavigationVC alloc]initWithRootViewController:chatResponse]; // [self pushVC:chatResponse]; chatResponse.view.backgroundColor = HexColorFromRGBA(0x000000, 0.55); chatResponse.definesPresentationContext = YES; nav.modalPresentationStyle = UIModalPresentationOverCurrentContext&UIModalPresentationOverFullScreen; [[ZCUtils getCurrentVC] presentViewController:nav animated:YES completion:^{ // chatResponse.view.backgroundColor = HexColorFromRGBA(0x000000, 0.55); }]; }); } } } failure:^(NSError *error) { }]; } }else if (dictData && ([[dictData objectForKey:@"id"]integerValue]==12||[[dictData objectForKey:@"id"]integerValue]==13||[[dictData objectForKey:@"id"]integerValue]==14)) {//只是用来做推送 不做任何处理 // NSLog(@"dictData is %@",dictData); NSDictionary* dict = dictData[@"data"]; YOUPAILPIMNotificationModel *model = [YOUPAILPIMNotificationModel mj_objectWithKeyValues:dict]; model.notifyType = [[dictData objectForKey:@"id"]integerValue]; NSDictionary *modelDic = @{@"model":model}; [[NSNotificationCenter defaultCenter]postNotificationName:@"PiaoPing" object:nil userInfo:modelDic]; }else if (dictData && ([[dictData objectForKey:@"id"]integerValue]==16)) {//上电视 // NSLog(@"dictData is %@",dictData); NSDictionary* dict = dictData[@"data"]; YOUPAILZHomeTVModel *model = [YOUPAILZHomeTVModel mj_objectWithKeyValues:dict]; NSDictionary *modelDic = @{@"model":model}; [[NSNotificationCenter defaultCenter]postNotificationName:@"VQUHomeTVDataNotification" object:nil userInfo:modelDic]; }else if (dictData && ([[dictData objectForKey:@"id"]integerValue]==17)) {//心动值变动通知 // NSLog(@"dictData is %@",dictData); NSDictionary* dict = dictData[@"data"]; NSString *from_uid = dict[@"from_uid"]; NSString *to_uid = dict[@"to_uid"]; NSString *score = dict[@"score"]; NSString *grade = dict[@"grade"]; NSString *uid = [NSString stringWithFormat:@"%@",from_uid]; if ([[LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id isEqual:[NSString stringWithFormat:@"%@",from_uid]]) { uid = [NSString stringWithFormat:@"%@",to_uid]; } [[YOUPAILZIntimateManager shareManager].intimateDict setObject:@{ @"score":[NSString stringWithFormat:@"%@",score], @"grade":[NSString stringWithFormat:@"%@",grade], } forKey:uid]; [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUIMIntimateChangeNotification" object:nil userInfo:@{@"uid":uid}]; }else if (dictData && ([[dictData objectForKey:@"id"]integerValue]==18)) {//心动升级通知 YOUPAILZUpgradeIntimacyLevelWindow *window = [[YOUPAILZUpgradeIntimacyLevelWindow alloc] init]; window.model = [YOUPAILZUpgradeIntimacyLevelModel mj_objectWithKeyValues:dictData[@"data"]]; [[LCTools getCurrentVC] TFPresentVC:window completion:nil]; }else if(dictData && [[dictData objectForKey:@"id"]integerValue]==19){ /// 红包消息 if (![LCSaveData getWhiteVersion]) { [self performSelector:@selector(showRedEnvelopesWindowWithModel:) withObject:[YOUPAILZRedEnvelopesModel mj_objectWithKeyValues:dictData[@"data"]] afterDelay:1.0f]; } }else if(dictData && [[dictData objectForKey:@"id"]integerValue]==99){ NSLog(@"随机速配成功!!!!!%@", dictData); [[NSNotificationCenter defaultCenter] postNotificationName:AUDIO_VIDEO_MATCHING object:nil userInfo:dictData]; } else if(dictData && [[dictData objectForKey:@"id"]integerValue]==20){ // [[NSNotificationCenter defaultCenter]postNotificationName:@"UpdateUserinfo" object:nil userInfo:nil]; NSDictionary* dict = dictData[@"data"]; [self youpaifrefreshDataWithIsAuth:[[dict objectForKey:@"is_auth"] integerValue]]; }else if (dictData && [[dictData objectForKey:@"id"]integerValue]== BBCustomSystomNotificationTypeWithCallVideo){ if ([LCSaveData getIsVideo]) { [ZCHUDHelper showTitle:@"正在视频中"]; continue ; }else{ if ([LCSaveData getWhiteVersion]) { [LCSaveData saveIsVideo:YES]; YOUPAILCVideoChatModel *videoModel = [YOUPAILCVideoChatModel mj_objectWithKeyValues:[dictData objectForKey:@"data"]]; YOUPAIBBVideoResponseVC * chatResponse = [[YOUPAIBBVideoResponseVC alloc]init]; chatResponse.youpaipvideoModel = videoModel; [self pushVC:chatResponse]; } } }else if (dictData && [[dictData objectForKey:@"id"]integerValue]==BBCustomSystomNotificationTypeWithCancelVideo){ if ([LCSaveData getWhiteVersion]) { /// 对方已取消视频电话,发出通知 [[NSNotificationCenter defaultCenter]postNotificationName:@"CancelVideo" object:nil userInfo:[dictData objectForKey:@"data"]]; } }else if (dictData && [[dictData objectForKey:@"id"]integerValue]==BBCustomSystomNotificationTypeWithRefuseCallVideo){ if ([LCSaveData getWhiteVersion]) { ///拒绝接听视频,发出通知 [[NSNotificationCenter defaultCenter]postNotificationName:@"RefuseCallVideo" object:nil userInfo:[dictData objectForKey:@"data"]]; } }else if (dictData && [[dictData objectForKey:@"id"]integerValue]==BBCustomSystomNotificationTypeWithAnswerCallVideo){ if ([LCSaveData getWhiteVersion]) { ///同意接听视频,发出通知 [[NSNotificationCenter defaultCenter]postNotificationName:@"AnswerCallVideo" object:nil userInfo:[dictData objectForKey:@"data"]]; } }else if (dictData && [[dictData objectForKey:@"id"]integerValue]== BBCustomSystomNotificationTypeWithHangupCallVideo){ if ([LCSaveData getWhiteVersion]) { ///挂断视频,发出通知 [[NSNotificationCenter defaultCenter]postNotificationName:@"HangupCallVideo" object:nil userInfo:[dictData objectForKey:@"data"]]; } } } [self.customSysNotiArray removeAllObjects]; } - (void)youpaifrefreshDataWithIsAuth:(NSInteger)isAuth{ YOUPAILCUserModel* userModel = [LCSaveModel getUserModel]; userModel.youpaipuserinfo.youpaipis_auth = isAuth; [LCSaveModel saveUserModel:userModel]; } - (void)showRedEnvelopesWindowWithModel:(YOUPAILZRedEnvelopesModel *)model{ // YOUPAILZRedEnvelopesWindow *window = [[YOUPAILZRedEnvelopesWindow alloc] init]; // window.model = model; // [[LCTools getCurrentVC] TFPresentVC:window completion:nil]; } - (void)pushVC:(UIViewController*)vc{ ZCBaseNavigationVC *nav = [[ZCBaseNavigationVC alloc]initWithRootViewController:vc]; //modify by leo fix bug ios13 // [[ZCUtils getCurrentVC].navigationController presentViewController:nav animated:YES completion:^{ // }]; // vc.view.backgroundColor = HexColorFromRGBA(0x000000, 0.55); nav.modalPresentationStyle = UIModalPresentationFullScreen; [[LCTools getNilPresentationController:[ZCUtils getCurrentVC]] presentViewController:nav animated:YES completion:^{ // vc.view.backgroundColor = HexColorFromRGBA(0x000000, 0.55); }]; } - (void)sendLocalNotificationWithMessage:(NIMMessage *)message { // if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive) { // NSLog(@"输出🍀\n%@",message.senderName); // return; // } NSLog(@"输出🍀\n%@",message.apnsPayload); NSLog(@"输出🍀\n%@",message.senderName); NSString *messageType = @""; if (message.messageType == NIMMessageTypeText) { message.apnsContent = @"收到一条信息"; }else if(message.messageType == NIMMessageTypeImage){ message.apnsContent = @"收到一张图片"; }else if(message.messageType == NIMMessageTypeAudio){ message.apnsContent = @"收到一段语音"; }else if(message.messageType == NIMMessageTypeVideo){ message.apnsContent = @"收到一段小视频"; } switch (message.messageType) { case NIMMessageTypeText: messageType = @"收到一条信息"; break; case NIMMessageTypeImage: messageType = @"收到一张图片"; break; case NIMMessageTypeAudio: messageType = @"收到一段语音"; break; case NIMMessageTypeVideo: messageType = @"收到一段小视频"; break; case NIMMessageTypeLocation: messageType = @"收到一个位置"; break; case NIMMessageTypeNotification: messageType = @"收到一个通知"; break; case NIMMessageTypeFile: messageType = @"收到一个文件"; break; case NIMMessageTypeTip: messageType = @"收到一个提醒"; break; case NIMMessageTypeRobot: messageType = @"收到一个客服消息"; break; case NIMMessageTypeCustom: messageType = @""; break; } UNUserNotificationCenter *notiCenter = [UNUserNotificationCenter currentNotificationCenter]; UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.badge = [NSNumber numberWithInteger: [UIApplication sharedApplication].applicationIconBadgeNumber + 1]; content.title = @"花蝶"; content.subtitle = @""; content.body = [NSString stringWithFormat:@"%@: %@",message.senderName, messageType]; content.sound = UNNotificationSound.defaultSound; UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.5 repeats:NO]; NSString *idf = [NSString stringWithFormat:@"%@-%@",messageType, @([NSDate date].timeIntervalSince1970)]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:idf content:content trigger:trigger]; [notiCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (!error) { NSLog(@"输出🍀\n添加通知(%@)成功",idf); } }]; } @end