需求:1.在页面Acell上显示当前未读消息个数,并且无论是后台还是前台,当有新消息时更新未读个数
2.声音 和 震动 开关 控制 ,实现 QQ 消息声音 和 震动 效果
未读消息实现方式:
在AppDelegate 里面 实现 代理方法,创建通知,当有新消息是 将未读个数以通知的方式发送给相应的控制器
代理:RCIMReceiveMessageDelegate [RCIM sharedRCIM].receiveMessageDelegate = self;
代码:收到消息代理,无论前台后台
pragma mark - 消息 与 声音
-
(void)onRCIMReceiveMessage:(RCMessage *)message left:(int)left{
int totalUnreadCount = [[RCIMClient sharedRCIMClient] getTotalUnreadCount];
[[NSNotificationCenter defaultCenter] postNotificationName:rongIM_HasMessage object:nil userInfo:@{@"totalUnreadCount":[NSString stringWithFormat:@"%d",totalUnreadCount]}];
}通知:
pragma mark - 有未读短消息
- (void)hasMessage:(NSNotification *)notification{
NSLog(@"**********8 您有未读短消息:%@",notification);
if ([notification.name isEqualToString:rongIM_HasMessage]) {
dispatch_async(dispatch_get_main_queue(), ^{
_messageNumberLabel.hidden = NO;
_messageNumberLabel.text = notification.userInfo [@"totalUnreadCount"];
});
}
}
ps:更新UI一定要调用主线程。不然会崩
pragma mark - 页面 出现 消失
-
(void)viewWillAppear:(BOOL)animated{
【super view 。。。】;
int number = [[RCIMClient sharedRCIMClient] getTotalUnreadCount];
if (number == 0) {_messageNumberLabel.hidden = YES;
}else{
_messageNumberLabel.hidden = NO; _messageNumberLabel.text = [NSString stringWithFormat:@"%d",number];
}
[self hidddenRedBadge];
}
声音 震动 :开关控制 userdefaul 写入 当前开关状态
AppDelete。m
代码:
//自定义声音
- (BOOL)onRCIMCustomAlertSound:(RCMessage *)message{
NSLog(@"走了自定义声音代码");
NSLog(@"当前声音存储状态 :%@",[[NSUserDefaults standardUserDefaults] objectForKey:setting_Voice]);
NSLog(@"当前震动存储状态 :%@",[[NSUserDefaults standardUserDefaults] objectForKey:setting_Vibrate]);
if ([[[NSUserDefaults standardUserDefaults] objectForKey:setting_Vibrate] isEqualToString:@"1"]) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}else{
}
if ([[[NSUserDefaults standardUserDefaults] objectForKey:setting_Voice] isEqualToString:@"1"]) {
return NO;
}else{
return YES;
}
}
ps:最好写成三木运算