【问题标题】:local notification of events that already happened in the past过去已经发生的事件的本地通知
【发布时间】:2012-04-27 13:22:28
【问题描述】:

我正在制作一个提醒应用程序,用户在其中创建一些带有日期和时间的事件,并通过选择触发日期来选择何时应该注意到它(此事件之前的时间间隔:现在、5、15、30 分钟前,等等)和重复间隔(从不、每天、每周、每月和每年)。问题是:当用户创建一个已经发生的事件时,例如事件应该在 4 月 10 日发生,而今天是 4 月 16 日,应该及时提醒他该事件,但用户在创建后立即收到有关此事件的通知它。所以它不应该发生。我怎样才能避免这种情况? 这是创建通知的方法

- (void)notificationWithNote:(Note *)scheduledNote deleteThisNotification:(BOOL)deleteNotification {

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    unsigned int unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit;
    NSDateComponents *comp = [calendar components:unitFlags fromDate:scheduledNote.date];

        ToDoItem *todoitem = [[ToDoItem alloc] init];
        todoitem.day = [comp day];
        todoitem.month = [comp month];
        todoitem.year = [comp year];
        todoitem.hour = [comp hour];
        todoitem.minute = [comp minute];
        todoitem.eventName = scheduledNote.event;

    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:todoitem.day];
    [dateComps setMonth:todoitem.month];
    [dateComps setYear:todoitem.year];
    [dateComps setHour:todoitem.hour];
    [dateComps setMinute:todoitem.minute];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];
    [dateComps release];


    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    if ([scheduledNote.remindTime intValue] == 1)
        localNotif.fireDate = itemDate;
    else
        localNotif.fireDate = [itemDate dateByAddingTimeInterval:-([scheduledNote.remindTime intValue]*60)];


    switch ([scheduledNote.repeatOption intValue]) {
        case 0:
            localNotif.repeatInterval = 0;
            break;
        case 1:
            localNotif.repeatInterval = NSDayCalendarUnit;
            break;
        case 2:
            localNotif.repeatInterval = NSWeekCalendarUnit;
            break;
        case 3:
            localNotif.repeatInterval = NSMonthCalendarUnit;
            break;
        case 4:
            localNotif.repeatInterval = NSYearCalendarUnit;
            break;
        default:
            break;
    }

    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ begins", nil), scheduledNote.event];
    localNotif.alertAction = NSLocalizedString(@"View Details", nil);
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
    NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:todoitem.eventName,ToDoItemKey, @"Timenote is coming", MessageTitleKey, nil];
    localNotif.userInfo = infoDict;

    if (deleteNotification)
        [[UIApplication sharedApplication] cancelLocalNotification:localNotif];
    else
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

    NSLog(@"fire date: %@",[localNotif.fireDate description]);
    [todoitem release];
    [localNotif release];
}

此错误仅在用户未选择任何重复间隔时发生。如果事件每天/每周/每月/每年重复,提醒会及时出现。所以如果localNotif.repeatInterval == 0

【问题讨论】:

    标签: objective-c ios uilocalnotification


    【解决方案1】:

    看起来,我找到了答案。如果问题只发生在没有重复间隔的情况下,则需要检查以下条件:如果 fireDate 小于当前日期并且如果没有重复间隔,则不应安排此通知。

    NSComparisonResult result = [localNotif.fireDate compare:[NSDate date]];
    
    if (((localNotif.repeatInterval == 0) && (result == NSOrderedAscending)) || deleteNotification)
    {
        [[UIApplication sharedApplication] cancelLocalNotification:localNotif];
    }
    else
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    

    【讨论】:

      猜你喜欢
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 2020-11-02
      • 2021-08-28
      相关资源
      最近更新 更多