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

标签 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];
    }
}

如何处理这个任务?

最佳答案

使用此方法来安排通知,其中 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];
}

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

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

关于iphone - 更新本地通知的开火日期并取消之前的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11137066/

相关文章:

iphone - 即使使用字符串初始化后,NSURL对象也为零

iphone - 有没有办法在不重新启动应用程序的情况下使 NSBundle 本地化缓存失效? [iOS]

python - 在 Pandas 中查找日期范围时出现问题

iphone - UILocalNotification 没有出现在 iOS7 中

ios - 无法访问时的本地通知和后台应用程序

ios - 本地通知立即触发,而不是按计划触发

ios - 在更新 iOS 程序注册 ID 时丢失

iphone - Xcode : UITextfield uneditable

linux - Bash:以人类可读的格式将 unix 时间四舍五入到最接近的分钟

swift - 测试当前时间是否在两个时间间隔之间