Xcode 每次关闭应用程序时播放的本地通知
在我的应用程序中,我有几行代码准备早上7点的本地通知。问题是每次我关闭应用程序时,都会显示通知。我只希望在设定的时间过后发生 在我的应用程序标识符背景中:Xcode 每次关闭应用程序时播放的本地通知,xcode,time,notifications,local,nscalendar,Xcode,Time,Notifications,Local,Nscalendar,在我的应用程序中,我有几行代码准备早上7点的本地通知。问题是每次我关闭应用程序时,都会显示通知。我只希望在设定的时间过后发生 在我的应用程序标识符背景中: NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit
NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[components setHour:7];
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init];
if (notifyAlarm) {
notifyAlarm.fireDate = [calendar dateFromComponents:components];
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
notifyAlarm.soundName = @"not.wav";
notifyAlarm.alertBody = @"test";
[app scheduleLocalNotification:notifyAlarm];
}
在我的应用程序中:
UIApplication *app = [UIApplication sharedApplication];
NSArray *oldNotifications = [app scheduledLocalNotifications];
if ([oldNotifications count] > 0) {
[app cancelAllLocalNotifications];
}
我认为问题在于[旧通知计数]。在实现NSLog以检查每次0时显示的金额后。这应该算上,对吗?
有什么帮助吗??:) 试试这段代码。如果要每天重复NSLocalNotification,则需要将repeatInterval设置为NSDayCalendar单位
NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[components setHour:7];
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init];
if (notifyAlarm)
{
notifyAlarm.fireDate = [calendar dateFromComponents:components];
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = NSDayCalendarUnit;
notifyAlarm.soundName = @"not.wav";
notifyAlarm.alertBody = @"test";
[app scheduleLocalNotification:notifyAlarm];
}
现在是早上7点以后吗?也许你想定在明天早上7点?是的,现在是早上7点以后。我不是说明天,我想让它显示notif。每天7点左右,假设现在是上午10点。您正在为3小时前创建通知。你需要在未来21小时内创建它。我只想让我的应用程序每天早上7点显示一个本地通知。问题是,每次我在过去的时间后关闭应用程序时,它都会再次显示。我只希望这一天发生一次。谢谢!正是我需要的!