需求: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:最好寫成三木運算