目錄:
1.會話列表頁沒有回話聊天的時候顯示內容自定義
2.實現(xiàn)收到消息時候,位于tabbar自定義小紅點的出現(xiàn)(并及時出現(xiàn))
3.#import "IQKeyboardManager.h"
與會話聊天鍵盤彈出的沖突
4.介紹一個做的時候比較麻煩的功能,關于消息的處理與查看的消息小紅點
5.當首次進入聊天時候,默認發(fā)送一條信息
1.會話列表頁沒有回話聊天的時候顯示內容自定義
只需要在繼承了
RCConversationListViewController
這個類中,賦值這個emptyConversationView
屬性即可
- (void)setupEmptyConversationView {
UIImageView *emptyImageView = [[UIImageView alloc] init];
emptyImageView.image = [UIImage imageNamed:@"facai"];
emptyImageView.size = CGSizeMake(kScreenWidth, kScreenHeight);
emptyImageView.center = self.view.center;
UIView * emptyView = [[UIView alloc] initWithFrame:self.view.bounds];
UILabel * emptyLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 350, 200, 50)];
emptyLabel.text = @"都沒有人跟我聊天";
UILabel * emptyLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(55, 200, 200, 50)];
emptyLabel1.text = @"藍瘦~";
UILabel * emptyLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 250, 200, 50)];
emptyLabel2.text = @"香菇~~";
[emptyView addSubview:emptyImageView];
[emptyView addSubview:emptyLabel];
[emptyView addSubview:emptyLabel1];
[emptyView addSubview:emptyLabel2];
self.emptyConversationView = emptyView;
}
當然這是非常簡單的功能甚至在融云開發(fā)文檔中com + f 搜索關鍵字就能找到
2.實現(xiàn)收到消息時候,位于tabbar自定義小紅點的出現(xiàn)(并及時出現(xiàn))
本人這里的實現(xiàn)是直接在tabbar上添加的原點圖片只要調整好frame完全沒有問題,而且采用[tabbar addSubView]
的方法不用擔心每次收到消息都會覆蓋添加的問題,所以移除的時候也是只需要移除一次就可以
// 創(chuàng)建imageView
_imageViewMessage = [[UIImageView alloc] init];
_imageViewMessage.frame = CGRectMake(kScreenWidth * 0.5, 8, 10, 10);
[_imageViewMessage setImage:[UIImage imageNamed:@"jindudian"]];
/// 接收到融云消息時調用
- (void)addRcMessage {
if (self.selectedIndex == 2) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tabBar addSubview:_imageViewMessage];
});
}
/// 其他地方處理融云消息,移除tabbar上的小紅點
[[NSNotificationCenter defaultCenter] addObserverForName:KRCMessageOver object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
[_imageViewMessage removeFromSuperview];
}];
這里的重點就是一定要利用線程才能達到及時添加的效果, 否則大概每次出現(xiàn)UI上的更新要有30s的延遲,個人猜測這個可能和運行循環(huán)有關,所以這里加線程來做就可以了.關于接收消息會調用的方法,大家看融云的開發(fā)文檔就可以了,而且如果你想加上數(shù)字角標也是可以的.
self.tabBarItem.badgeValue = @"3";
ps:這里貼一個自己查資料時候的小烏龍 .. 算是絕處逢生吧
3.記錄一個自己遇到的犯二的小點,關于
#import "IQKeyboardManager.h"
這個是很好用的框架具體使用方法的帖子介紹
導入IQKeyboardManager后束析,默認所有的頁面都有了這個功能艳馒,如果你在哪一個界面不想有這個效果可以在當前界面控制器的生命周期方法中進行設置:
#import <IQKeyboardManager.h>
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[IQKeyboardManager sharedManager].enable = NO;
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[IQKeyboardManager sharedManager].enable = YES;
}
這里需要自定義一個繼承
RCConversationViewController
的類MKConversationViewController
進行跳轉并在自定義的類的視圖生命周期里寫上面的方法
//重寫RCConversationListViewController的onSelectedTableRow事件
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType
conversationModel:(RCConversationModel *)model
atIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%@%@",model.conversationTitle,model.targetId);
RCConversationViewController *conversationVC = [[MKConversationViewController alloc]init];
conversationVC.conversationType = model.conversationType;
conversationVC.targetId = model.targetId;
conversationVC.title = model.conversationTitle;
[self.navigationController pushViewController:conversationVC animated:YES];
}
4.這里介紹一個做的時候比較麻煩的功能,就是關于消息的處理與查看
這是一個個人感覺比較麻煩的處理地方,因為要實現(xiàn)一旦接受到消息所有界面右上角的聊天入口都要顯示小紅點,而且任意一個界面進入消息列表頁面查看過消息,其他頁面的小紅點都要消失,就像gif圖里這種效果,當然這是一個不是很完美的效果,按照QQ微信這種做法應該是進入到具體的消息會話中消息提醒才會消息,這里做的是只要進入到消息列表頁就視為消息查看過的狀態(tài)取消小紅點.
a,發(fā)送消息的時機,當我們的聊天列表頁面出現(xiàn)在與我們的主Window重合的時候就可以視為跳轉到了此頁面,即相當于此消息已處理,發(fā)送一個通知消息供其他頁面接收做UI上的處理
/// 判斷聊天頁面是不是跟主窗口重疊 ,如果是就發(fā)送已經處理了信息的消息
- (void)setupMessageRead {
CGRect windowRect = [UIApplication sharedApplication].keyWindow.bounds;
CGRect myViewRect = [self.view convertRect:self.view.bounds toView:nil];
BOOL overlap = CGRectIntersectsRect(windowRect, myViewRect);
if (overlap) {
// NSLog(@"======chongdie ====重疊 ====");
[[NSNotificationCenter defaultCenter] postNotificationName:KRCMessageOver object:nil];
}
}
這里涉及到坐標系轉換的方法來判斷是否跳轉相關頁面,很方便
b.給要跳轉的頁面增加一個BOOL
屬性來判斷消息處理的情況,達到第一次進入界面的時候做是否有紅點的處理
// 判斷是否有為添加的新消息 調整rightItembar
@property(assign,nonatomic ) BOOL messageNew;
-----------------------
/// 融云監(jiān)聽是否改變聊天入口的狀態(tài)
[[NSNotificationCenter defaultCenter] addObserverForName:KRCMessage object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
self.messageNew = YES;
}];
/// 融云監(jiān)聽是否改變聊天入口的狀態(tài)
[[NSNotificationCenter defaultCenter] addObserverForName:KRCMessageOver object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
self.messageNew = NO;
}];
/// 融云信息查看傳到下一個界面
details.messageNew = self.messageNew;
[self.navigationController pushViewController:details animated:YES];
c.然后在即將顯示的見面里通過_messageNew
這個BOOL
值做處理
/// 導航欄根據(jù)融云消息按鈕
if (self.messageNew) {
_message = [UIBarButtonItem BarButtonItemWithBackgroudImageName:@"xiaoxitixing" highBackgroudImageName:nil target:self action:@selector(messageCenter)];
}else {
_message = [UIBarButtonItem BarButtonItemWithBackgroudImageName:@"xiaoxi" highBackgroudImageName:nil target:self action:@selector(messageCenter)];
}
self.navigationItem.rightBarButtonItem = _message;
這里做的是切換
rightBarButtonItem
的圖片,不再是加小圓點了.效果是一樣的.
d.實時監(jiān)聽頁面消息的變化
/// 融云消息實時改變UIBarButtonItem的顯示
[[NSNotificationCenter defaultCenter] addObserverForName:KRCMessage object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[self setupRCMessageImageDetail];
}];
[[NSNotificationCenter defaultCenter] addObserverForName:KRCMessageOver object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[self readRCMessageImageDetail];
}];
/// 未查看消息要做的事.| 同樣在改變`rightBarButtonItem`時候也要注意線程
- (void)setupRCMessageImageDetail {
dispatch_sync(dispatch_get_main_queue(), ^{
_message = [UIBarButtonItem BarButtonItemWithBackgroudImageName:@"xiaoxitixing" highBackgroudImageName:nil target:self action:@selector(messageCenter)];
});
}
/// 已查看消息要做的事.
- (void)readRCMessageImageDetail {
_message = [UIBarButtonItem BarButtonItemWithBackgroudImageName:@"xiaoxi" highBackgroudImageName:nil target:self action:@selector(messageCenter)];
}
e.移除通知
#pragma mark - 移除通知
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:KRCMessageOver object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:KRCMessage object:nil];
NSLog(@"===詳情頁死了====");
}
5.當首次進入聊天時候,默認發(fā)送一條信息,比如我們需要知道買家查看的哪個商品并從哪個商品的詳情進入到與賣家的聊天的,這樣方便知道買家對哪個商品感興趣
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//判斷是否與剛剛查看的商品是同一個,避免每次進入都發(fā)送同樣的內容
if (self.isSame) {
return;
}
RCTextMessage * text = [RCTextMessage new];
text.content = [NSString stringWithFormat:@"正在查看: %@ 價格: %@元/米",_modell.group_title,_modell.group_price];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self sendMessage:text pushContent:@"遠程推送消息"];
});
}
上面這個是我在百度查到的因為官方文檔里這里有個坑,如下:
這里提示消息要和發(fā)送的消息區(qū)分開,因為提示消息僅僅買家能夠看得到,僅僅相當于給買家一個提示,而并非也會發(fā)給賣家
到此基本完成了.整體思路僅供參考交流,暫無demo.大神勿噴.如有不足或者錯誤望斧正.原創(chuàng),轉載請注明.