Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何区分当前用户发送的PNMessage与其他用户发送的PNMessage?_Ios_Pubnub - Fatal编程技术网

Ios 如何区分当前用户发送的PNMessage与其他用户发送的PNMessage?

Ios 如何区分当前用户发送的PNMessage与其他用户发送的PNMessage?,ios,pubnub,Ios,Pubnub,我目前正在使用PubNub将类似聊天的功能构建到应用程序中。按照演示项目中的示例,我将以下内容添加到我的viewDidLoad中,以侦听我的频道接收到的消息: [[PNObservationCenter defaultCenter] addMessageReceiveObserver:self withBlock:^(PNMessage *message) { NSLog(@"message %@", me

我目前正在使用PubNub将类似聊天的功能构建到应用程序中。按照演示项目中的示例,我将以下内容添加到我的
viewDidLoad
中,以侦听我的频道接收到的消息:

[[PNObservationCenter defaultCenter] addMessageReceiveObserver:self withBlock:^(PNMessage *message) {
                                             NSLog(@"message %@", message);
                                             [self DisplayInLog: [NSString stringWithFormat:@"[%@]: %@",message.channel.name, message.message]];
                                             [self showReceivedMessage:message];
                                         }];
我的问题是,当当前用户使用
[PubNub sendMessage:text to channel:self.currentChannel]
发送消息时,侦听器会接收到消息(如预期的那样),但是我无法区分当前用户发送的、其他人发送的、接收者接收的
PNMessage
。我应该如何处理这个问题而不会变得太粗鲁(例如,比较消息的内容、消息何时发送等等)

谢谢

@daspianist

您必须添加存储用户标识符的字段,并在客户端通过从将作为消息发送的字典中检索值来检查谁是消息的发送者。 例如:

[[PNObservationCenter defaultCenter] addMessageReceiveObserver:self withBlock:^(PNMessage *message) {

    NSLog(@"message %@", message);
    if (![[message.message valueForKey:@"sender"] isEqualToString:@"Bob"]) {

        [self DisplayInLog: [NSString stringWithFormat:@"[%@]: %@",message.channel.name, message.message]];
        [self showReceivedMessage:message];
    }
}];

[PubNub sendMessage:@{@"message":@"This is actual message which we want to send. It can be any Objective-C type.", @"sender":@"Bob"} toChannel:self.currentChannel];

在上面的示例中,如果消息是从“Bob

字典中的发送消息发送的,则它不应处理该消息

NSDictionary *message = [[NSDictionary alloc] initWithObjectsAndKeys:@"HELLLLOO",@"text",@"Kiran",@"sender", nil];
    [PubNub sendMessage:message toChannel:[PNDataManager sharedInstance].currentChannel
    withCompletionBlock:^(PNMessageState state, id object) {
}

我现在的处境也一样。我正在尝试使用
requestHistoryForChannel
,您可以在其中设置from:和to:值。在这种情况下,您将获得一个包含消息的数组,但我不确定这是否是正确的方法。我发现了一个黑客解决方法:因为接收的
PNMessage.message
是一种
[NSDictionary class]
,而刚发送的
PNMessage.message
是一种
[NSString class]
,因此,这可以作为一种区分的方法。当然,更正式的方式会有帮助。我错了,我的版本不起作用,因为from和to用于PNDate,而不是通道。您是否也可以告诉我们,对于
requestHistoryForChannel
with block,推荐的解决方案是什么?因为它使用PNMessage对象而不是PNMessage创建了一个数组,所以我不能使用这个概念,也不能基于字典键进行枚举来获取一些指定的消息。谢谢谢谢hanks@moonlight-发送和检索我自己特定的一组键值对做到了这一点。@sabin您可以使用相同的概念。一旦您将发件人的身份发送到邮件中,历史记录将由那些“正确”的邮件填充,当您将拉出历史记录时,您将能够迭代邮件列表,并在考虑发件人身份的情况下对其进行筛选。