Chompas
Chompas

Reputation: 563

iPhone - Get Remaining time from UILocalNotification

I want to know if there's a way to get the remaining time from a Local Notification previously set.

Let's say I make something like this:

    //Creating Notification
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = someTime;
    localNotif.alertBody = @"Your parking time will expire in 10 mins";
    localNotif.alertAction = @"View";
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];

So then I fully close my app. I reopen it and I want to show some message saying how much time is left for the notification to launch.

Thanks in advance.

Upvotes: 1

Views: 624

Answers (2)

A. Adam
A. Adam

Reputation: 3134

When you call the below method like [self timeRemaning]; it will give you how many seconds renaming if there is any notification registered. This will work only for one registered notification.

-(int)timeRemaning{
    NSArray *checkNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
     if (![checkNotifications count] == 0) {
    UILocalNotification *localNotification = [checkNotifications objectAtIndex:0];
    NSString*   notifdate=[localNotification.fireDate description];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss ZZ"];
    NSCalendar *c = [NSCalendar currentCalendar];
    NSDate *d1 = [NSDate date];
    NSDate *d2 = [dateFormatter dateFromString:notifdate];
    NSDateComponents *components = [c components:NSSecondCalendarUnit fromDate:d1 toDate:d2 options:0];
    NSInteger diff = components.second;
         int temp = diff;
    return temp;

     }else{
         return 0;
     }
}

Upvotes: 0

David Neiss
David Neiss

Reputation: 8237

You are supposed to be able to get a list of the currently scheduled notifications with the UIApplication scheduledLocalNotifications property. Maybe get this and do some math on the time?

Upvotes: 2

Related Questions