Ios 本地通知在错误的时间发送
我想在iOS中的特定时间每天发送2个本地通知 但是通知在错误的时间发送,而且它一天发送多次,而不是一次 这是我的代码片段Ios 本地通知在错误的时间发送,ios,objective-c,iphone,ipad,uilocalnotification,Ios,Objective C,Iphone,Ipad,Uilocalnotification,我想在iOS中的特定时间每天发送2个本地通知 但是通知在错误的时间发送,而且它一天发送多次,而不是一次 这是我的代码片段 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // are you running on iOS8? if ([application respondsToSelector:@selecto
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// are you running on iOS8?
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
}
else // iOS 7 or earlier
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
application.applicationIconBadgeNumber = 0;
//This if will be called only once....
if ([prefs stringForKey:@"Notification"] == nil)
{
//... 1st notification ...
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:11];
[components setMinute:24];
// Gives us today's date but at 11am
NSDate *next11am = [calendar dateFromComponents:components];
if ([next11am timeIntervalSinceNow] < 0) {
// If today's 9am already occurred, add 24hours to get to tomorrow's
next11am = [next11am dateByAddingTimeInterval:60*60*24];
}
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = next11am;
notification.alertBody = @"Notification 1.";
// Set a repeat interval to daily
notification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
//... 2nd Notification ...
NSDate *now1 = [NSDate date];
NSCalendar *calendar1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components1 = [calendar1 components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now1];
[components1 setHour:18];
[components1 setMinute:30];
// Gives us today's date but at 9am
NSDate *next6pm = [calendar dateFromComponents:components];
if ([next6pm timeIntervalSinceNow] < 0) {
// If today's 6pm already occurred, add 24hours to get to tomorrow's
next6pm = [next6pm dateByAddingTimeInterval:60*60*24];
}
UILocalNotification *notification1 = [[UILocalNotification alloc] init];
notification1.fireDate = next6pm;
notification1.alertBody = @"Notification 2.";
// Set a repeat interval to daily
notification1.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notification1];
}
}
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
//你在用iOS8吗?
if([application respondsToSelector:@selector(registerUserNotificationSettings:)]))
{
UIUserNotificationSettings*设置=[UIUserNotificationSettings设置类型:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound)类别:无];
[应用程序注册表通知设置:设置];
}
else//iOS 7或更早版本
{
UIRemoteNotificationType myTypes=UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application RegisterForRemotionTificationTypes:myTypes];
}
application.applicationBadgeNumber=0;
//此if将只调用一次。。。。
if([prefs stringForKey:@“Notification”]==nil)
{
//…第一次通知。。。
NSDate*现在=[NSDate日期];
NSCalendar*calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents*components=[日历组件:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:now];
[组件设定小时:11];
[分钟:24];
//给我们今天的日期但在上午11点
NSDate*next11am=[日历日期fromComponents:components];
如果([next11am TimeIntervalncesInnow]<0){
//如果今天的上午9点已经发生,则增加24小时到达明天的上午9点
next11am=[next11am日期通过添加时间间隔:60*60*24];
}
UILocalNotification*通知=[[UILocalNotification alloc]init];
notification.fireDate=next11am;
notification.alertBody=@“通知1。”;
//将重复间隔设置为“每日”
notification.repeatInterval=NSDayCalendarUnit;
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
//…第二次通知。。。
NSDate*now1=[NSDate日期];
NSCalendar*calendar1=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents*components1=[calendar1组件:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:now1];
[组件1设定小时:18];
[组件1设置分钟:30];
//给我们今天的日期但在上午9点
NSDate*next6pm=[calendar dateFromComponents:components];
如果([next6pm TimeIntervalncesInnow]<0){
//如果今天的下午6点已经发生,那么再加上24小时就可以到达明天的下午6点
next6pm=[next6pm dateByAddingTimeInterval:60*60*24];
}
UILocalNotification*notification1=[[UILocalNotification alloc]init];
通知1.fireDate=next6pm;
notification1.alertBody=@“通知2。”;
//将重复间隔设置为“每日”
notification1.repeatInterval=NSDayCalendarUnit;
[[UIApplication sharedApplication]scheduleLocalNotification:notification1];
}
}
我哪里做错了?请帮忙
提前感谢您选中了
[prefs stringForKey:@“Notification”]
,但您尚未定义prefs
,因此它可能为零,并且您似乎从未设置通知的值,因此也可能会丢失该值。这些东西中的任何一个都意味着你正在添加你不希望添加的内容
您正在调用scheduleLocalNotification:
,但您没有调用cancelLocalNotification:
或cancelAllLocalNotifications
,因此,根据用户使用应用程序的方式或您进行测试的方式,您可以轻松地同时添加多个通知。使用scheduledLocalNotification
检查当前计划的内容
您可能还应该对日历使用currentCalendar
,因为您需要尊重用户设置。在我看来,日期逻辑也是错误的。我是否正确地理解了正在创建一个日期,给定一个时间,创建一个没有时间的新日期,但OP希望这两个日期具有相同的时间?@SimonMcLoughlin计算和设置的11/6/9时间之间确实存在一些混淆。更详细地看,创建了2次,但没有实际使用,相反,这两个通知都设置在未定义的下一个上午next9am
。请参阅我的新编辑@Wain:I在代码中定义了prefs
,我没有在此处添加不必要的代码,上面的if条件我已经在那里写了这个//这个if将只被调用一次….
当用户在那里关闭时,有一个开关我已经写了//通知关闭[[UIApplication sharedApplication]cancelAllLocalNotifications];[UIApplication sharedApplication].applicationIconBadgeNumber=0代码>好,它现在做什么?另外,请注意,您不应该通过添加时间间隔来使用日期,您应该在组件中添加一天并生成一个新日期…仍然存在相同的问题,当我记录计划通知时,它会显示许多通知。比如{fire date=2015年5月7日星期四下午12:35:47印度标准时间,时区=Asia/Kolkata(GMT+5:30)偏移量19800,repeat interval=NSCalendarUnitDay,repeat count=UILocalNotificationInfiniteRepeatCount,next fire date=2015年5月13日星期三下午12:35:47印度标准时间,user info=(null)}