在iOS上获取消息时,Pubnub消息receiveDate为nil

时间:2014-07-22 00:40:30

标签: ios pubnub

Pubnub获取邮件时,其receiveDate为零。以下是我正在做的事情的一个例子:

void (^completionBlock)(NSArray *pnMessages, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error) = ^(NSArray *pnMessages, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error) {
for (PNMessage *pnMessage in pnMessages) {
    NSDate *date = pnMessage.receiveDate; // <-- date here is nil (pnMessage.date also)

    // some logic here with date.
}

// fetch messages.
[PubNub requestFullHistoryForChannel:_channel withCompletionBlock:completionBlock];

为了检索每条消息的日期,还有什么其他想法吗?

2 个答案:

答案 0 :(得分:1)

您需要使用适当的方法来确保填充这些值。默认情况下, PubNub 服务不会向您提供此信息,您必须使用此类特定方法(还有其他指定方法):

+ (void)requestFullHistoryForChannel:(PNChannel *)channel 
                  includingTimeToken:(BOOL)shouldIncludeTimeToken
                 withCompletionBlock:(PNClientHistoryLoadHandlingBlock)handleBlock

此方法明确请求 PubNub 服务,以便在此邮件从客户端传递到云时返回日期。

答案 1 :(得分:-1)

第一步:以字典格式发送您的PubNub消息。您可以添加key来保存发送邮件的日期。

 NSDictionary *dictionary = @{
                           @"sender":senderSample,
                           @"message":messageSample;
                           @"date":dateString;
                           };

[PubNub sendMessage: dictionary toChannel:sampleChannel];

第二步:检索邮件,现在您收到包含日期的邮件。

[PubNub requestFullHistoryForChannel: sampleChannel withCompletionBlock:^(NSArray *message, PNChannel *channel, PNDate *fromDate, PNDate *toDate, PNError *error)

   NSLog(@"Here's my new messages with date %@", message);


}];

如果您运行应用程序并检查控制台,您将看到包含message个对象的PNMessage数组。现在,您必须使用message属性(包含date key)而不是receiveDate。每个新的消息对象都将包含您可以使用的日期。