【问题标题】:Using PubNub History API with iOS在 iOS 中使用 PubNub History API
【发布时间】:2014-07-31 07:51:30
【问题描述】:

如何使用 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 发生在从历史记录中接收到的消息之后。

【问题讨论】:

  • 你正在运行什么代码调用 subscribePostChannels: withError:?
  • 我在上面的代码中看到的主要问题:1)订阅将不会运行,除非您已经通过 connect() 方法连接到 PubNub。您是否已经在未提供的代码中连接到其他地方? 2)如果您已经连接,那么您嵌入历史调用的连接完成块将永远不会执行,因为您已经连接。
  • 是否将 [PubNub requestHistoryForChannel:channel 从:[PNDate dateWithDate:lastLogin] 移动到:nil];从连接成功块内到连接块外,在您的订阅调用之后,让一切正常吗?
  • 是的,我已经连接上了。我在 applicationDidFinishLaunchingWithOptions 中连接第一件事
  • 好的,如果您已经连接,那么您必须从第二个连接请求的成功块中删除历史调用。这是否为您解决了问题?

标签: ios pubnub


【解决方案1】:

我的第二个问题是显示所有消息而不是只显示我想要的消息,因为 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 帮助我解决这个问题。

【讨论】:

    【解决方案2】:

    这似乎对我有用,如果它对你有用,请告诉我:

    在我的 ViewController.m 中:

    PNConfiguration *myConfig = [PNConfiguration configurationForOrigin:@"pubsub.pubnub.com" publishKey:@"demo" subscribeKey:@"demo" secretKey:@"demo"];
    
    [PubNub setConfiguration:myConfig];
    [PubNub connectWithSuccessBlock:^(NSString *origin) {
    
        PNLog(PNLogGeneralLevel, self, @"{BLOCK} PubNub client connected to: %@", origin);
    
        PNChannel *myChannel = [PNChannel channelWithName:@"a" shouldObservePresence:YES];
        [PubNub requestHistoryForChannel:myChannel from:nil to:nil limit:100];
    
    }
    
                         errorBlock:^(PNError *connectionError) {
        if (connectionError.code == kPNClientConnectionFailedOnInternetFailureError) {
            PNLog(PNLogGeneralLevel, self, @"Connection will be established as soon as internet connection will be restored");
        }
    
        // UIAlert code, etc
    
    }];
    

    在我的 AppDelegate 中:

    - (void)pubnubClient:(PubNub *)client didReceiveMessageHistory:(NSArray *)messages forChannel:(PNChannel *)channel
        startingFrom:(NSDate *)startDate to:(NSDate *)endDate {
    
    PNLog(PNLogGeneralLevel, self, @"PubNub client received history for %@ starting from %@ to %@: %@", channel,
            startDate, endDate, messages);
    

    }

    我看到了委托的输出,例如:

    2014-07-31 11:12:41.076 PubNubDemo[70859:60b] AppDelegate (0x8e25eb0) PubNub client received history for PNChannel(0x93261e0) a starting from PNDate (0x9330d90) <date: 2014-07-31 18:10:43 +0000; time token: 14068302439622999> to PNDate (0x9330e30) <date: 2014-07-31 18:12:40 +0000; time token: 14068303601434709>: (
    "PNMessage (0x9329650): <message: ***********.... 1861 - 2014-07-31 11:10:43, date: (null), channel: a>",
    "PNMessage (0x9330830): <message: ************... 1862 - 2014-07-31 11:10:45, date: (null), channel: a>",
    "PNMessage (0x9330850): <message: *************.. 1863 - 2014-07-31 11:10:46, date: (null), channel: a>",
    "PNMessage (0x9330870): <message: **************. 1864 - 2014-07-31 11:10:47, date: (null), channel: a>",
    

    有关代表的更多信息在这里:

    https://github.com/pubnub/objective-c/tree/master/iOS#--voidpubnubclientpubnub-client-didreceivemessagehistorynsarray-messages-forchannelpnchannel-channel-startingfrompndate-startdate-topndate-enddate

    这里有更多关于观察者的信息:

    https://github.com/pubnub/objective-c/tree/master/iOS#observation-center

    这是否会引导您朝着正确的方向前进?

    杰瑞米

    【讨论】:

    • 对我不起作用。我将编辑原始帖子以显示我如何尝试调用它。
    • 感谢您编辑您的原始帖子以澄清。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    相关资源
    最近更新 更多