【发布时间】:2018-09-26 04:31:51
【问题描述】:
我有一个小时数组hoursArray 和分钟数组minutesArray,我现在从系统中获取current date 我有包含当月元素的数组,这意味着如果有 30 天在四月份,我在数组中插入的 hoursArray/minutesArray 中将有 30 小时/分钟,并且我将当前日期作为数组的索引。
我所做的是通知在当天触发,但直到我每天使用应用程序才会在第二天触发,因为当我每天使用应用程序之前,当我切换到后台模式和通知响铃时会调用触发时间方法.
现在我希望在日期更改时自动触发通知,即使我有几天不使用该应用程序,这些东西都应该在appDelegate 的didEnterBackground... 方法中
我关注this answer,但它每天用于同一时间但我希望每天与基于当前日期的数组索引的数组不同的时间(这意味着当天,例如今天是 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...,当我退出我的应用程序时,通知方法只会被调用一次。不是吗??以及当我一周没有打开应用程序但我想收到通知时,我如何收到每日通知,将如何调用方法?是在后台模式下每次都调用方法吗?
有什么办法可以让我安排通知,当第一个通知完成时,第二个应该被触发,如果第二个完成,那么第三个应该被触发,等等在后台模式下,即使我没有打开应用程序一周??应根据数组中给出的当前日期和时间触发通知。
更新 我什至设置了刷新功能,它每 10000 秒刷新一次触发器,但它不起作用。
我还添加了 significantTimechanges ,如果有午夜变化,但它不起作用到这里是我如何定义的。
【问题讨论】:
-
你有多少天的数据?下次您何时调用您的服务来刷新数据,因为响应中没有指定日期。
-
@TheTiger 我有几个月的数据,日期有效期为一个月
-
您的意思是您有 3 月的 31 天数据和 2 月的 28 天数据?
-
你有多少数据?还有无限的几个月 --> 几年。
-
首先如果用户不打开应用,那么你什么都做不了,所以忘记这个成就。这种情况应该在服务器端处理,所以最好使用推送通知而不是本地通知。
标签: ios objective-c unnotificationtrigger