在做即时通讯时,自定义消息

前言:

(有可能您是高手,并不觉的怎么样,我第一次搞即时通讯,最初还是懵逼状态的,感觉有点变态☺,项目用的是阿里百川的旺信集成的哦)

我们的产品需要的效果是“当用户通过输入框输入特殊字的时候,在消息中用蓝色标记这个字体,并实现点击跳转”。

实现思路:

经过一天多的思考,最终确定下了解决方案,第一,检测消息的发送周期(需要设置代理,遵循YWMessageLifeDelegate),当文本消息将要发送的时候,检测是否含有关键字,(参考了demo中,关于关键字屏蔽的原理,就像你聊天时输入骂人的话会被取消发送并本地提示你含有违规字体一样。)当检测到含有关键字的时候,用自己定义的消息体发送,当没有的时候,走正常的逻辑。第二,自定义完消息体后,也要根据demo中所写的一样,自己实现viewmodel(所用到的数据)和view(所要显示的样式),这样才能将三者联系到一起,就能实现需求了。

检测生命周期的方法:

/**

*  监听自己发送的消息的生命周期

*/

- (void)exampleListenMyMessageLife

{

[[self.ywIMKit.IMCore getConversationService] addMessageLifeDelegate:self forPriority:YWBlockPriorityDeveloper];

}

/// 当你监听了消息生命周期,IMSDK会回调以下两个函数

- (YWMessageLifeContext *)messageLifeWillSend:(YWMessageLifeContext *)aContext

{

/// 你可以通过返回context,来实现改变消息的能力

if ([aContext.messageBody isKindOfClass:[YWMessageBodyText class]]) {

NSString *text = [(YWMessageBodyText *)aContext.messageBody messageText];

if ([text rangeOfString:@"发轮功事件"].location != NSNotFound) {

YWMessageBodySystemNotify *bodyNotify = [[YWMessageBodySystemNotify alloc] initWithContent:@"消息包含违禁词语"];

[aContext setMessageBody:bodyNotify];

NSDictionary *params = @{kYWMsgCtrlKeyClientLocal:@{kYWMsgCtrlKeyClientLocalKeyOnlySave:@(YES)}};

[aContext setControlParameters:params];

return aContext;

}

}

return nil;

}

- (void)messageLifeDidSend:(NSString *)aMessageId conversationId:(NSString *)aConversationId result:(NSError *)aResult

{

/// 你可以在消息发送完成后,做一些事情,例如播放一个提示音等等

}

这段是demo中的代码,应该都能看懂,就在将要发送的代理中,根据自己的需求,返回自己的消息体即可。下面是添加自定义的view和viewmodel的方法:

/// 设置用于显示自定义消息的ChatView

/// ChatView一般从ViewModel中获取已经解析的数据,用于显示

[self setHook4BubbleView:^YWBaseBubbleChatView *(YWBaseBubbleViewModel *viewModel) {

if ([viewModel isKindOfClass:[SPCallingCardBubbleViewModel class]]) {

SPCallingCardBubbleChatView *chatView = [[SPCallingCardBubbleChatView alloc] init];

return chatView;

}

else if ([viewModel isKindOfClass:[SPGreetingBubbleViewModel class]]) {

SPGreetingBubbleChatView *chatView = [[SPGreetingBubbleChatView alloc] init];

return chatView;

}

else if ([viewModel isKindOfClass:[SPBubbleViewModelCustomize class]]) {

SPBaseBubbleChatViewCustomize *chatView = [[SPBaseBubbleChatViewCustomize alloc] init];

return chatView;

}

return nil;

}];

/// 设置用于显示自定义消息的ViewModel

/// ViewModel,顾名思义,一般用于解析和存储结构化数据

[self setHook4BubbleViewModel:^YWBaseBubbleViewModel *(idmessage) {

if ([[message messageBody] isKindOfClass:[YWMessageBodyCustomize class]]) {

YWMessageBodyCustomize *customizeMessageBody = (YWMessageBodyCustomize *)[message messageBody];

NSData *contentData = [customizeMessageBody.content dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *contentDictionary = [NSJSONSerialization JSONObjectWithData:contentData

options:0

error:NULL];

NSString *messageType = contentDictionary[kSPCustomizeMessageType];

if ([messageType isEqualToString:@"CallingCard"]) {

SPCallingCardBubbleViewModel *viewModel = [[SPCallingCardBubbleViewModel alloc] initWithMessage:message];

return viewModel;

}

else if ([messageType isEqualToString:@"Greeting"]) {

SPGreetingBubbleViewModel *viewModel = [[SPGreetingBubbleViewModel alloc] initWithMessage:message];

return viewModel;

}

else {

SPBubbleViewModelCustomize *viewModel = [[SPBubbleViewModelCustomize alloc] initWithMessage:message];

return viewModel;

}

}

return nil;

}];

实现了这三个,就能完成这个需求了!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,838评论 0 9
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,099评论 19 139
  • Lancy's Blog Blog Archives About MeTwitterWeiboGitHubRSS ...
    其实也没有阅读 5,556评论 0 24
  • 我们常常会听说 Objective-C 是一门动态语言,那么这个「动态」表现在哪呢?我想最主要的表现就是 Obje...
    Ethan_Struggle阅读 2,237评论 0 7
  • 我梦见我们终于在一起 我们相拥在蓝色沙发上 甜蜜的交流着以前的误会 掩藏在冷漠背后的深情 你流着泪,我久久吻着你 ...
    文森林木阅读 153评论 0 0