Ram
Ram

Reputation: 406

How to check for Expired UILocalNotifications?

Hi can anyone please Explain How to check for Expired UILocalNotifications and cancel the notifications?

Upvotes: 1

Views: 2081

Answers (4)

Eric
Eric

Reputation: 16931

If your UILocalNotifications increment the application icon badge number (i.e. the number in the red circle on the top right of the app's icon), then there is a ridiculously simple way to check for unacknowledged UILocalNotifications: just check what the current applicationIconBadgeNumber is:

- (void)applicationWillEnterForeground:(UIApplication *)application
{        
    int unacknowledgedNotifs = application.applicationIconBadgeNumber;
    NSLog(@"I got %d unacknowledged notifications", unacknowledgedNotifs);
    //do something about it...

    //You might want to reset the count afterwards:
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

}

Upvotes: 0

EmptyStack
EmptyStack

Reputation: 51374

"You can not cancel an expired notification, because it is already expired/fired".

When you schedule the notification, if the fireDate is nil or a past date then the notification will be fired immediately. So, schedule the notification for the future dates alone.

By the time applicationDidEnterBackground delegate is called all the notifications(which you refer as "Expired Notifications") would have got fired.

Once a notification is fired it will be automatically removed if it is not an recurring notification.

To cancel a local notification on a date

Use the following code to cancel a notification if the last date has reached. Call this method either from didReceiveLocalNotificaiton: method or didFinishLaunchingWithOptions: method.

- (void)stop:(UILocalNotification *)localNotif ifLastDate:(NSDate *)lastDate {

    NSTimeInterval ti = 24*60*60; // One day
    NSDate *expiryDate = [lastDate dateByAddingTimeInterval:ti];
    NSDate *nextFireDate = [localNotif.fireDate dateByAddingTimeInterval:ti];

    if ([nextFireDate compare:expiryDate] == NSOrderedDescending) {

        [[UIApplication sharedApplication] cancelLocalNotification:localNotif];
    }
}

The didFinishLaunchingWithOptions: method,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    // Other Codes Here

    Class cls = NSClassFromString(@"UILocalNotification");

    if (cls != nil) {

        UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        if (notification) [self stop:notification ifLastDate:aDate]; // aDate is the last date you want to check
    }

    // Other Codes Here

    return YES;
}

The didReceiveLocalNotification: method,

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

    [self stop:notification ifLastDate:aDate]; // aDate is the last date you want to check
}

Note that you can cancel a local notification only when the app is running. You can not cancel a notification if the app is not running. The above code will cancel the notification only if the the app is opened by tapping the action button after the notification is fired on lastDate. If not, the notification will be cancelled when the app is opened by tapping action button on a notification that fires after the lastDate.

Upvotes: 1

Tendulkar
Tendulkar

Reputation: 5540

in the below mentioned delegate method you can check the date

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notif {
    if(notif.fireDate  Compare:[NSDate date]==NSOrderAscending)
    NSLog(@"Expired");
}

Upvotes: 0

thatguy
thatguy

Reputation: 313

Can you elaborate on Expired??? You can get the notification that are before expiry time(may b current time) by giving the timeinterval range. You can definitely cancel notification using

- (void)cancelLocalNotification:(UILocalNotification *)notification

Upvotes: 0

Related Questions