在创建自定义消息时设置 senderUserInfo及mentionedInfo。

发送自定义消息时,对senderUserInfo,mentionedInfo赋值,但是接收消息方看到senderUserInfo,mentionedInfo的值是nil。
发布时间: 2015-12-14 15:13

回答:

需要在自定义消息的m文件中对senderUserInfo进行编解码操作,可模仿下面代码实现:

- (NSData *)encode {


    NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];

    [dataDict setObject:self.content forKey:@"content"];

    if (self.extra) {

        [dataDict setObject:self.extra forKey:@"extra"];

    }


    if (self.senderUserInfo) {

        NSMutableDictionary *__dic = [[NSMutableDictionary alloc] init];

        if (self.senderUserInfo.name) {

            [__dic setObject:self.senderUserInfo.name forKeyedSubscript:@"name"];

        }

        if (self.senderUserInfo.portraitUri) {

            [__dic setObject:self.senderUserInfo.portraitUri forKeyedSubscript:@"portrait"];

        }

        if (self.senderUserInfo.userId) {

            [__dic setObject:self.senderUserInfo.userId forKeyedSubscript:@"id"];

        }

        [dataDict setObject:__dic forKey:@"user"];

    }

   if (self.mentionedInfo) {

        NSMutableDictionary *mentionedInfodic = [[NSMutableDictionary alloc] init];

        [mentionedInfodic setObject:@(self.mentionedInfo.type) forKeyedSubscript:@"type"];

        if (self.mentionedInfo.type == RC_Mentioned_Users) {

             [mentionedInfodic setObject:self.mentionedInfo.userIdList forKeyedSubscript:@"userIdList"];

        }

        [mentionedInfodic setObject:self.mentionedInfo.mentionedContent forKeyedSubscript:@"mentionedContent"];

        [dataDict setObject:mentionedInfodic forKey:@"mentionedInfo"];

    }

    // NSDictionary* dataDict = [NSDictionary

    // dictionaryWithObjectsAndKeys:self.content, @"content", nil];

    NSData *data = [NSJSONSerialization dataWithJSONObject:dataDict options:kNilOptions error:nil];

    return data;

}


- (void)decodeWithData:(NSData *)data {

    __autoreleasing NSError *__error = nil;

    if (!data) {

        return;

    }

    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&__error];

    RCDictionary *json = [[RCDictionary alloc] initWithDictionary:dictionary];


    if (json) {