每天在数组Objective C ios中给出的动态时间自动触发本地通知
我有一个小时hoursArray
arrays和分钟分钟阵minutesArray
和我现在从系统获得current date
我有arrays谁拥有当月的元素它意味着如果有4月30天将有30个小时/我在数组中插入的hoursArray / minutesArray中的分钟数,并且我将当前日期作为数组的索引。
我所做的是在当天触发通知,但在我每天使用app之前不会触发第二天,因为当我转向后台模式和通知响铃时,我会在每天使用app之前调用触发时间方法。
现在我想要在日期更改时自动触发通知,即使我有一天没有使用该应用程序,这些所有内容都应该在didEnterBackground...
方法
我已经按照这个答案 ,但它每天都在同一时间使用但我希望每天从数组基于当前日期的数组索引不同的时间(这意味着当前日期,例如今天是4月19日,小时和分钟数组的索引应该是19 )。
这是我的数组的样子
小时数组= [9,10,11,13,14,11,17,2,15,5 ……等等]
分数= [23,00,04,58,59,12,01,33 ……等等]
appdelegate.m
方法调用
- (void)applicationDidEnterBackground:(UIApplication *)application { [self.viewController triggerAutomaticallyDaily]; [self applicationSignificantTimeChange:application]; [self refreshAlarm]; }
viewcontroller.m
方法
-(void)triggerAutomaticallyDaily { NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ; NSDate *now = [NSDate date]; NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now]; [components setHour:hoursArray[currentDay]]; //currentDay the todays date is 16 I am getting current date from system. [components setMinute:minutesArray[currentDay]]; UILocalNotification *notification = [[UILocalNotification alloc]init]; notification.fireDate = [calendar dateFromComponents:components]; notification.repeatInterval = NSDayCalendarUnit; [notification setAlertBody:@"This is your task time"]; // notification.soundName = UILocalNotificationDefaultSoundName; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }
每个午夜之后的重要时间变化, Appdelegate
方法
-(void)applicationSignificantTimeChange:(UIApplication *)application { [self.viewController triggerAutomaticallyDaily]; }
在Appdelegate
刷新警报方法
- (void)refreshLabel { //refresh the alarm on the main thread dispatch_async(dispatch_get_main_queue(),^{ [self.viewController triggerAutomaticallyDaily]; }); // check every 10000s [self performSelector:@selector(refreshLabel) withObject:nil afterDelay:10000]; }
看看didEnterBackground...
当我退出我的应用程序时,通知方法将只调用一次。 不是?? 当我甚至没有打开应用程序一周但我想收到通知时,我可以收到每日通知,如何调用方法? 是每次在后台模式下调用的方法吗?
是否有任何方法可以安排通知,并且当第一次通知完成时,第二次应该被触发,如果第二次完成,那么第三次应该被触发,等等在后台模式,即使我不打开应用程序一周? ? 应根据数组中给出的当前日期和时间触发通知。
更新我甚至放置了刷新function,刷新function在每10000秒后刷新触发器,但它不起作用。
我还添加了significantTimechanges
,如果有午夜的变化,但它不适用于这里是我如何定义所有。
重要说明:如果应用程序长时间处于终止状态或后台,则开发人员无法执行任何操作。 所以这不可能做到。
您的要求有很多问题:
1.1:您在一项服务中, for a month
, for a year
或多少days data
?
1.2:为什么不push
?
1.3:为什么您希望每天安排local notification
,而您可以一次安排多个?
1.4:当app
未打开(由用户杀死)时,无法对app
进行更改,因此在这种情况下您无法执行任何操作。
现在来看看我们能做什么:
2.1:考虑到您正在获取月度数据。 因此,您应该通过增加day component
并设置相应的时间(from your arrays by day index)
一次性调度for loop
的所有local notifications
。 如果用户在一个月内打开一次应用程序(他有90%的可能性),那么你的运气就好了,那么你可以向服务器请求下个月的数据并做同样的事情。
2.2:为什么不在服务器端处理它并使用push notifications
而不是local
。 服务器人员可以为每分钟配置一个cron作业,这将为用户提取所有小时和分钟,并将发送push
。
我的建议不是去local
而是最大限度你可以选择2.1
(一次安排所有月份的通知)。
只需使用下面的语句而不是NSCalendar
UILocalNotification *notification = [[UILocalNotification alloc]init]; notification.fireDate = [[NSDate date] dateByAddingTimeInterval:(hours*3600)+(Mins*60)];
//时间间隔单位应为秒
notification.repeatInterval = NSDayCalendarUnit; [notification setAlertBody:@"This is your task time"]; [[UIApplication sharedApplication] scheduleLocalNotification:notification];
正如其他人所说,更好的技术是一次安排所有通知。 这就是我的工作。 但是,系统限制为64个待处理的本地通知。 在您的情况下,预定通知应每天触发64天。 我有一个更难的问题,因为我需要每15分钟安排一次通知。 因此,如果我的用户在16小时内没有回复,他们将不会收到进一步的通知(64/4 = 16)。 另请注意,UILocalNotification已弃用,因此您应使用UNNotificationRequest。 请注意,UNCalendarNotificationTrigger中的标识符对于每个请求必须是唯一的。
content.categoryIdentifier = @"com.nelsoncapes.localNotification"; NSDate *today = [NSDate date]; NSDate *fireDate = [today dateByAddingTimeInterval:interval]; // first extract the various components of the date NSCalendar *calendar = [NSCalendar currentCalendar]; NSInteger year = [calendar component:NSCalendarUnitYear fromDate:fireDate]; NSInteger month = [calendar component:NSCalendarUnitMonth fromDate:fireDate]; NSInteger day = [calendar component:NSCalendarUnitDay fromDate:fireDate]; NSInteger hour = [calendar component:NSCalendarUnitHour fromDate:fireDate]; NSInteger minute = [calendar component:NSCalendarUnitMinute fromDate:fireDate]; NSDateComponents *components = [[NSDateComponents alloc]init]; components.year = year; components.month = month; components.day = day; components.hour = hour; components.minute = minute; … // construct a calendarnotification trigger and add it to the system UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier: identifier content:content trigger:trigger]; [center addNotificationRequest:request withCompletionHandler:^(NSError *error){ if(error){ NSLog(@"error on trigger notification %@", error); } }];