如何删除UILocalNotification重复间隔?

时间:2013-04-26 07:09:10

标签: objective-c uilocalnotification

我添加了UILocalNotification,重复间隔为一分钟。 怎么删除它?

3 个答案:

答案 0 :(得分:1)

[[UIApplication sharedApplication] cancelAllLocalNotifications];

只需复制方法didLaunchWithOptionsviewDidLoad,甚至是下一个UILocalNotification代码。

答案 1 :(得分: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;
        }
    }
}

答案 2 :(得分:0)

尝试

notification.repeatInterval = NSCalendarUnit(rawValue: 0)

此代码位于Swift中。在objective-c中尝试使用相同的东西。