开发者

How to check for Expired UILocalNotifications?

开发者 https://www.devze.com 2023-04-05 22:46 出处:网络
Hi can anyone please Explain How to check for Expi开发者_高级运维red UILocalNotifications and cancel the notifications?\"You can not cancel an expired notification, because it is already expired/fired

Hi can anyone please Explain How to check for Expi开发者_高级运维red UILocalNotifications and cancel the notifications?


"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.


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


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");
}


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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号