Peter Warbo
Peter Warbo

Reputation: 11700

Find out when a repeating UILocalNotification was fired

I have a UILocalNotification that is set to start at 2012-06-18 10:00 with a repeat interval of 1 minute.

At 2012-06-18 10:05 5 notifications would have been triggered. The user would then choose notification number 3. The notification should have been fired at 2012-06-18 10:02.

In my - application:didReceiveLocalNotification: method. How can I programatically get this fire date from notification number 3 which should be 2012-06-18 10:02. I know I can get the intitial/start fireDate property from the UILocalNotification but I'm not interested in that. I'm interested to know the fireDate of this repeating notification (not the intial/start fireDate).

If someone can explain to me how to find out which repeating UILocalNotification was fired without parsing the - description of the notification I will give you some of my hard earned bounty.

Upvotes: 3

Views: 2839

Answers (3)

Oleg Trakhman
Oleg Trakhman

Reputation: 2082

Q:

If someone can explain to me how to find out which repeating UILocalNotification was fired without parsing the - description of the notification I will give you some of my hard earned bounty.

A: The easiest way:

- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSDateComponents *comps = [[NSCalendar currentCalendar] components:notification.repeatInterval fromDate:notification.fireDate toDate:[NSDate date] options:0];
    NSLog(@"Notification #%d", [comps minute] + 1);
}

Q: How to get that "next fire date" from description without parsing?

A: There is private/undocumented function for this: nextFireDateForLastFireDate

- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"next: %@", [notification nextFireDateForLastFireDate:[NSDate date]]);
}

Q: Maybe there is some kind of count in notifications? property or method?

A: There's undocumented properties:

@property (assign, nonatomic) int totalRepeatCount;
@property (assign, nonatomic) int remainingRepeatCount;

But they seem to have always same value: UILocalNotificationInfiniteRepeatCount. Both of them is managed by operating system, so overwriting of this properties or underlying instance variable does nothing. Moreover, nothing changes inside UILocalNotifications from firing to firing, there's no way to distinguish one UILocalNotification from another (except address in memory). Next fireDate is calculated basing on current time.

Upvotes: 0

Pochi
Pochi

Reputation: 13459

So I have given this a bit more thought, and as I said in my comment it doesnt seem possible because of the way notifications are handled.

You create one and fire immediately or schedule it.

A notification is just the storage of some information which means it is only meant to be read for its properties.

The application receives the notification through

application:didReceiveLocalNotification:

In where only the UILocalNotification itself is passed. This notification ONLY has a bunch of properties which were set at the start.

The repeat interval is only used to re-notify the user, it doesnt change what the notification has inside.

This comes to the conclusion that for the behavior you expect to achieve you would have to fire different notifications if you expect to pass different information or perform different actions.

(Sub-classing is also not useful as explained here https://stackoverflow.com/a/8583329/1068522)

Upvotes: 1

waylonion
waylonion

Reputation: 6976

The best alternate solution to getting the fireDate of the UILocalNotification is to calculate the date.
Given that you have an initial fireDate, you can use the repeated interval to calculate the fireDate of a given notification.

1.Start with the initial fire date
2.Get the notification number/index of the notification you have
3.Multiply the repeated interval by the index and add it onto your initial fire date

However, date calculating, as mentioned in the below link, is tricky due to time zones and "other nasty things."
Here's a helpful link:
How to grab the NEXT fire date from a UILocalNotification object

And of course, there is the final, fall back solution of parsing the description method. Using [notification.fireDate description] However, as you probably know, it is NEVER a good idea to do so because the formatting may change in the future, thus breaking your code.

Hope this helped!

Edit: Example: Okay, so say my first initial fireDate was 2012-06-18 10:00
I know that my repeated interval is every ONE minute, right?
So say, that the user taps uilocalnotification number 3, then that means TWO minutes should have passed!
THEREFORE, the time for that specific notification is: 2012-06-18 10:02
Does this make sense?

Upvotes: 1

Related Questions