如何使用History API接收频道的历史记录?在文档中,它说对[PubNub requestHistoryForChannel:myChannel from:nil to:nil limit:100 reverseHistory:YES];的调用成功返回一个数组,但是我的编译器断言返回值是空的。该方法和相关方法的文档说它们“从历史记录中获取消息”,但是我似乎无法弄清楚消息的获取位置。消息发送到的委托方法是否存在?请帮帮我。

谢谢

编辑

现在正在接收消息,但不是按照指定的时间间隔。我正在接收频道上的所有消息。

- (void)subscribePostChannels:(NSArray *)results withError:(NSError *)error
{
    if (!error) {
        static int SECONDS_IN_TEN_DAYS = 864000;
        for (PFObject *post in results) {
            if ([post.createdAt timeIntervalSinceNow] > (-1) * SECONDS_IN_TEN_DAYS) {
                [self.channelsToSubscribe addObject:post.objectId];
                NSString *pushChannel = [NSString stringWithFormat:@"channel_%@", post.objectId];
                [[PFInstallation currentInstallation] addUniqueObject:pushChannel forKey:@"channels"];
            }
        }
        NSArray *channels = [PNChannel channelsWithNames:self.channelsToSubscribe];
        [PubNub subscribeOnChannels:channels];
        // Now retrieve messages
        NSDate *lastLogin = [PFUser currentUser][@"lastActive"];
        for (PNChannel *channel in channels) {
            [PubNub requestHistoryForChannel:channel from:[PNDate dateWithDate:lastLogin] includingTimeToken:YES withCompletionBlock:^(NSArray *array, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error) {
                if (!error) {
                    NSLog(@"Last Active: %@", [PFUser currentUser][@"lastActive"]);
                    if (channel == [channels lastObject]) {
                        [PFUser currentUser][@"lastActive"] = [NSDate date];
                        [[PFUser currentUser] saveInBackground];
                    }
                } else {
                    NSLog(@"Error Fetching History: %@", error);
                }
            }];
        }
    } else {
        NSLog(@"Error finding messages. Post channels not subscribed.");
    }
}



- (void)subscribePostChannels:(NSArray *)results withError:(NSError *)error
{
    static int SECONDS_IN_TEN_DAYS = 864000;
    for (PFObject *post in results) {
        if ([post.createdAt timeIntervalSinceNow] > (-1) * SECONDS_IN_TEN_DAYS) {
            [self.channelsToSubscribe addObject:post.objectId];
            NSString *pushChannel = [NSString stringWithFormat:@"channel_%@", post.objectId];
            [[PFInstallation currentInstallation] addUniqueObject:pushChannel forKey:@"channels"];
        }
    }
    NSArray *channels = [PNChannel channelsWithNames:self.channelsToSubscribe];
    [PubNub subscribeOnChannels:channels];
    // Now retrieve messages
    for (PNChannel *channel in channels) {
        NSDate *lastLogin = [PFUser currentUser][@"lastActive"];
        [PubNub requestHistoryForChannel:channel from:[PNDate dateWithDate:lastLogin] to:nil];
    }
}

日志消息指示从历史记录接收到消息之后发生lastActive。

最佳答案

我的第二个问题是显示所有消息,而不仅仅是显示我想要的消息,这是因为PubNub History API的设置如下:

[PubNub requestHistoryForChannel:myChannel from:nil to:nil limit:100];
from:日期被视为开始回溯的日期,然后您检索该日期之前的所有邮件,直到to:日期为止。我以为from:应该是较早的日期,事实并非如此。

因此,对于我来说,从上次登录起直到我需要使用的当前日期为止,我都会收到所有消息:
[PubNub requestHistoryForChannel:myChannel from:nil to:[PNDate dateWithDate:lastLogin] limit:100];

感谢PubNub支持人员Craig的帮助。

10-05 20:32
查看更多