如何使用History API接收频道的历史记录?在文档中,它表示对[PubNub requestHistoryForChannel:myChannel from:nil to:nil limit:100 reverseHistory:YES];
的调用在成功时返回一个数组,但我的编译器断言返回值为void。该方法和相关方法的文档说它们“从历史记录中获取消息”,但我似乎无法弄清楚消息的提取位置。是否有发送消息的委托方法?请帮帮我。
谢谢
修改
现在正在接收消息,但不是根据指定的时间间隔。我收到了频道上的所有消息。
- (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。
答案 0 :(得分:3)
我的第二个问题是显示所有消息而不仅仅是我想要的消息,因为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支持帮助我解决这个问题。
答案 1 :(得分:0)
这似乎对我有用,让我知道它是否适合你:
在我的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#observation-center
这是否指导您朝着正确的方向前进?
geremy