0

When fetching messages from Pubnub, its receiveDate is nil. Here's an example attached on what I'm doing:

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];

Any ideas something else has to be done to retrieve the date for each message?

Diego Acosta
  • 1,737
  • 3
  • 16
  • 21

2 Answers2

1

You need to use appropriate methods to make sure those values to be filled. By default PubNub service won't provide this information to you and you have to use specific methods like this one (there is other designated methods):

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

This method explicitly request PubNub service to return date when this message has been delivered from client to the cloud.

Serhii Mamontov
  • 4,942
  • 22
  • 26
-1

First step: Send your PubNub message in a dictionary format. You can add a key that holds the date when you send the message.

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

[PubNub sendMessage: dictionary toChannel:sampleChannel];

Second step: Retrieve messages, and now you have messages with date.

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

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


}];

If you run your app and check the console you will see the message array, that contains PNMessage objects. Now you have to work with the message property (which contains the date key) instead of the receiveDate. Every new message object will contains the date you can use.

rihekopo
  • 3,241
  • 4
  • 34
  • 63