在iOS中使用PubNub历史API
如何使用历史API接收通道的历史记录?在文档中,它说调用在iOS中使用PubNub历史API,ios,pubnub,Ios,Pubnub,如何使用历史API接收通道的历史记录?在文档中,它说调用[PubNub requestHistoryForChannel:myChannel from:nil to:nil limit:100 reverseHistory:YES]成功返回一个数组,但我的编译器断言返回值无效。该方法和相关方法的文档中说它们“从历史中获取消息”,但我似乎不知道这些消息被提取到哪里。是否存在将消息发送到的委托方法?请帮帮我 多谢各位 编辑 现在正在接收消息,但不是按照指定的时间间隔。我正在接收频道上的所有消息 -
[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发生在从历史记录接收到的消息之后。这似乎对我有效,如果对您有效,请告诉我:
在my 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>",
2014-07-31 11:12:41.076 PubNubDemo[70859:60b]AppDelegate(0x8e25eb0)PubNub客户端收到了从PNDate(0x9330d90)到PNDate(0x9330e30)的PNChannel(0x93261e0)a的历史记录:(
“PNMessage(0x9329650):”,
“PNMessage(0x9330830):”,
“PNMessage(0x9330850):”,
“PNMessage(0x9330870):”,
有关代表的更多信息,请点击此处:
还有更多关于观察员的信息:
这能指引你正确的方向吗
geremy我的第二个问题是,显示的是所有消息,而不是我想要的消息,这是因为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 support的Craig帮我解决了这个问题。对我来说不起作用。我将编辑原始帖子,以展示我是如何尝试调用它的。感谢您编辑原始帖子以澄清问题。您正在运行哪些调用subscribePostChannels的代码:withError:?我在您的上述代码中看到的主要问题:1)订阅不会运行,除非您已经通过connect()方法连接到PubNub。您是否已使用未提供的代码连接到其他地方?2) 如果您已经连接,则嵌入历史记录调用的连接完成块将永远不会执行,因为您已经连接。是否将[PubNub requestHistoryForChannel:channel from:[PNDate dateWithDate:lastLogin]移动到:nil];从连接成功块内部到连接块外部,在您的订阅呼叫之后,使事情正常进行?是的,我已经连接。我连接ApplicationIDFinishLaunching中的第一件事是使用选项OK,如果您已经连接,则必须在第二个连接请求的成功块中删除历史记录调用。这能为您解决问题吗?