2

I am looking to display usernames alongside messages in a chatroom using quickblox. I was hoping to simply embed the sender name in the custom parameters of the message but the params never make it through to my chatroomDidRecieveMessage.

I copied the code from the example with no luck.

 [message setCustomParameters:@{@"playSound": @YES}];

Also, can't seem to find a pattern in the senderID/recipientID that can go along with the message. So questions is, what is the best option for getting the sender data when a message is received?

Working on iOS...

4

1 回答 1

1

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

#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];

并接收消息:

-(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];
}
于 2013-10-02T09:09:36.563 回答