更新本地通知的开火日期并取消之前的通知

时间:2012-06-21 11:22:18

标签: iphone date uilocalnotification

我知道有一些问题herethere有关如何删除可能是全部或特定通知的本地通知。我也经历了local notification class reference,并找到了一些方法,如重复时间间隔,开火日期,警报体,时区等......但我无法找到有关如何修改开火日期的某些信息已经设定好了。假设用户设置了今天的日期和下午4:50的通知,但是如果用户希望修改设置的日期/时间,则发生的情况是两种情况下都会触发通知。就编程伦理而言,这是一个错误!

实际上我想要的是必须取消之前的通知,即必须将日期修改为已编辑的通知,并且应在新日期设置并触发通知。

这是我设置通知的示例代码:

- (void)setNotification
{
    //Set notification after confirmation of saved data

    Class cls = NSClassFromString(@"UILocalNotification");
    reminderNotification = [[cls alloc] init];

    if (cls != nil) 
    {        
       NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
       [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
       NSDate *notificationDate = [dateFormat dateFromString:textField2.text];
       reminderNotification.fireDate = notificationDate;
       reminderNotification.timeZone = [NSTimeZone defaultTimeZone];
       NSString *reminderText = [NSString stringWithFormat:@"%@ 's %@ on %@",textField.text,textField1.text,strDate];
       reminderNotification.alertBody = reminderText;
       reminderNotification.alertAction = @"View";
       reminderNotification.soundName = @"lazy_afternoon.mp3";
       reminderNotification.applicationIconBadgeNumber = 1;
       NSDictionary *userDict = [NSDictionary dictionaryWithObject:self.textField1.text forKey:kReminder];
       reminderNotification.userInfo = userDict;
       [[UIApplication sharedApplication] scheduleLocalNotification:reminderNotification];
       [reminderNotification release];
    }
}

如何处理这项任务?

2 个答案:

答案 0 :(得分:18)

使用此方法安排通知,其中notificationID必须是唯一的

 -(void) scheduleNotificationForDate:(NSDate *)date AlertBody:(NSString *)alertBody ActionButtonTitle:(NSString *)actionButtonTitle NotificationID:(NSString *)notificationID{

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = date;
    localNotification.timeZone = [NSTimeZone localTimeZone];
    localNotification.alertBody = alertBody;
    localNotification.alertAction = actionButtonTitle;
    localNotification.soundName = @"yourSound.wav";

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
    localNotification.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

使用此方法取消具有该通知标识的特定通知

- (void)cancelLocalNotification:(NSString*)notificationID {
    //loop through all scheduled notifications and cancel the one we're looking for
    UILocalNotification *cancelThisNotification = nil;
    BOOL hasNotification = NO;

    for (UILocalNotification *someNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        if([[someNotification.userInfo objectForKey:notificationID] isEqualToString:notificationID]) {
            cancelThisNotification = someNotification;
            hasNotification = YES;
            break;
        }
    }
    if (hasNotification == YES) {
        NSLog(@"%@ ",cancelThisNotification);
        [[UIApplication sharedApplication] cancelLocalNotification:cancelThisNotification];        
    }
}

参考: UILocalNotification

答案 1 :(得分:3)

设置通知后,唯一的编辑方式是取消旧通知并重新创建另一个通知,这样您就可以这样做,搜索现有通知并取消通知。

for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        if([[aNotif.userInfo objectForKey:@"id"] isEqualToString:nId]) {
            [[UIApplication sharedApplication]cancelLocalNotification:aNotif];
        }
    }

然后创建新通知。