如何检查过期的UILocalNotifications?

时间:2021-12-11 07:16:28

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

嗨,任何人都可以请解释如何检查过期的UILocalNotifications并取消通知?

4 个解决方案

#1


1  

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

安排通知时,如果fireDate为nil或过去的日期,则会立即触发通知。因此,仅为未来日期安排通知。

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

在调用applicationDidEnterBackground委托时,所有通知(您称为“过期通知”)都会被触发。

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.

如果已达到最后日期,请使用以下代码取消通知。从didReceiveLocalNotificaiton:方法或didFinishLaunchingWithOptions:方法调用此方法。

- (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,

didFinishLaunchingWithOptions:方法,

- (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,

didReceiveLocalNotification:方法,

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

请注意,只有在应用程序运行时才能取消本地通知。如果应用未运行,您无法取消通知。仅当在lastDate上触发通知后点击操作按钮打开应用程序时,上述代码才会取消通知。如果没有,通过点击lastDate之后触发的通知上的操作按钮,打开应用程序时将取消通知。

#2


0  

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

你能详细说明Expired ???您可以通过给出timeinterval范围来获取到期时间之前的通知(可能是当前时间)。你绝对可以取消通知

- (void)cancelLocalNotification:(UILocalNotification *)notification

- (void)cancelLocalNotification:(UILocalNotification *)通知

#3


0  

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

#4


0  

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:

如果您的UILocalNotifications增加了应用程序图标徽章编号(即应用程序图标右上角红色圆圈中的数字),那么有一种非常简单的方法可以检查未确认的UILocalNotifications:只需检查当前applicationIconBadgeNumber是什么:

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

}

#1


1  

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

安排通知时,如果fireDate为nil或过去的日期,则会立即触发通知。因此,仅为未来日期安排通知。

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

在调用applicationDidEnterBackground委托时,所有通知(您称为“过期通知”)都会被触发。

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.

如果已达到最后日期,请使用以下代码取消通知。从didReceiveLocalNotificaiton:方法或didFinishLaunchingWithOptions:方法调用此方法。

- (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,

didFinishLaunchingWithOptions:方法,

- (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,

didReceiveLocalNotification:方法,

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

请注意,只有在应用程序运行时才能取消本地通知。如果应用未运行,您无法取消通知。仅当在lastDate上触发通知后点击操作按钮打开应用程序时,上述代码才会取消通知。如果没有,通过点击lastDate之后触发的通知上的操作按钮,打开应用程序时将取消通知。

#2


0  

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

你能详细说明Expired ???您可以通过给出timeinterval范围来获取到期时间之前的通知(可能是当前时间)。你绝对可以取消通知

- (void)cancelLocalNotification:(UILocalNotification *)notification

- (void)cancelLocalNotification:(UILocalNotification *)通知

#3


0  

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

#4


0  

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:

如果您的UILocalNotifications增加了应用程序图标徽章编号(即应用程序图标右上角红色圆圈中的数字),那么有一种非常简单的方法可以检查未确认的UILocalNotifications:只需检查当前applicationIconBadgeNumber是什么:

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

}