首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ChatRoom消息的自定义参数

ChatRoom消息的自定义参数
EN

Stack Overflow用户
提问于 2013-10-02 04:28:19
回答 1查看 723关注 0票数 2

我希望显示用户名旁边的消息在聊天室使用quickblox。我希望简单地将发送者名称嵌入到消息的自定义参数中,但参数永远不会通过我的chatroomDidRecieveMessage

我从示例中复制了代码,但没有成功。

代码语言:javascript
复制
 [message setCustomParameters:@{@"playSound": @YES}];

此外,在senderID/recipientID中似乎找不到可以与消息一起使用的模式。所以问题是,在收到消息时,获取发送者数据的最佳选择是什么?

在iOS上工作...

EN

回答 1

Stack Overflow用户

发布于 2013-10-02 17:09:37

更好的方法是将所有发送者的信息封装到JSON中,例如,让我们在message中发送用户的位置:

代码语言:javascript
复制
#define kLatitude @"latitude"
#define kLongitude @"longitude"
#define kMessage @"message"

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"12.23423424" forKey:kLatitude];
[dict setObject:@"-2.13123333" forKey:kLongitude];
[dict setValue:@"Hello, this is my location" forKey:kMessage];

// to JSON:
NSError *error = nil;
NSData* nsdata = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

// send message
NSString* jsonString =[[NSString alloc] initWithData:nsdata encoding:NSUTF8StringEncoding];     
[[QBChat instance] sendMessage:jsonString toRoom:self.currentRoom];

并接收消息:

代码语言:javascript
复制
-(void)chatRoomDidReceiveMessage:(QBChatMessage *)message fromRoom:(NSString *)roomName{
    // JSON parsing
    NSData *data = [message.text dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    NSString *message = [jsonDict objectForKey:kMessage];
    NSString *latitude = [jsonDict objectForKey:kLatitude];
    NSString *longitude = [jsonDict objectForKey:kLongitude];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19125298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档