gpt4 book ai didi

objective-c - 过去已经发生的事件的本地通知

转载 作者:行者123 更新时间:2023-11-29 13:37:29 25 4
gpt4 key购买 nike

我正在制作一个提醒应用程序,用户可以在其中创建一些带有日期和时间的事件,并通过选择触发日期来选择何时应该注意到它(此事件之前的间隔:现在、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

问题是实际的

最佳答案

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

NSComparisonResult result = [localNotif.fireDate compare:[NSDate date]];

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

关于objective-c - 过去已经发生的事件的本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10170302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com