0

我添加了一个重复间隔为一分钟的 UILocalNotification。如何删除它?

4

3 回答 3

1

[[UIApplication sharedApplication] cancelAllLocalNotifications];

只需将其复制到方法下,didLaunchWithOptions甚至复制viewDidLoad到您的下一个UILocalNotification代码中即可。

于 2013-05-05T02:11:34.967 回答
0
- (BOOL)scheduleNotificationOn:(NSDate*) fireDate
                          text:(NSString*) alertText
                        action:(NSString*) alertAction
                         sound:(NSString*) soundfileName
                   launchImage:(NSString*) launchImage
                       andInfo:(NSDictionary*) userInfo
{
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = fireDate;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.alertBody = alertText;
    localNotification.alertAction = alertAction;
    localNotification.soundName = soundfileName != nil ? soundfileName : UILocalNotificationDefaultSoundName;
    localNotification.alertLaunchImage = launchImage;
    localNotification.userInfo = userInfo;

    for (UILocalNotification *notification in [self allLocalNotifications]) {
        if ([notification.fireDate timeIntervalSinceDate:localNotification.fireDate] == 0 &&
            [notification.alertBody isEqualToString:localNotification.alertBody]
            ) {
            return NO;
        }
    }
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    [localNotification release];
    return YES;
}
- (NSArray *)allLocalNotifications
{
    return [[UIApplication sharedApplication] scheduledLocalNotifications];
}
- (void)cancelLocalNotification:(UILocalNotification *)notification
{
    [[UIApplication sharedApplication] cancelLocalNotification:notification];
}

- (void)cancelAllLocalNotifications
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

您应该创建一个唯一标识符来删除通知,例如 alertBody.. 然后

- (void)cancelLocalNotificationByAlertBody:(NSString *)alertBody
{
    for (UILocalNotification *notification in [self allLocalNotifications]) {
        if ([notification.alertBody isEqual:alertBodyString] ) {
            [self cancelLocalNotification:notification];
            break;
        }
    }
}
于 2013-04-26T07:20:28.530 回答
0

Try with

notification.repeatInterval = NSCalendarUnit(rawValue: 0)

This code is in Swift. Try with the same thing in objective-c.

于 2015-08-27T22:29:18.203 回答