[英]Using PubNub History API with iOS
如何使用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:
date被視為開始回溯的日期,然后您檢索該日期之前的所有消息,直到to:
date。 我以為是from:
應該是較早的日期,情況並非如此。
因此,對於我來說,從上次登錄起直到我需要使用的當前日期為止,我都會收到所有消息:
[PubNub requestHistoryForChannel:myChannel from:nil to:[PNDate dateWithDate:lastLogin] limit:100];
感謝PubNub支持人員Craig的幫助。
這似乎對我有用,請讓我知道是否對您有用:
在我的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.