每天在数组Objective C ios中给出的动态时间自动触发本地通知 [英] Trigger local notifications automatically daily on dynamic time given in arrays Objective c ios

查看:91
本文介绍了每天在数组Objective C ios中给出的动态时间自动触发本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小时数组 hoursArray 和分钟数组 minutesArray 我得到当前日期现在来自系统我拥有当前月份元素的数组,这意味着如果4月份有30天就会有30小时/分钟的小时数/分钟数我已经有了插入到数组中,我将当前日期作为数组的索引。

I have an array of hours hoursArray and array of minutes minutesArray and I am getting the current date from the system now I have the arrays who have the elements of the current month it means if there are 30 days in April there will be 30 hours/minutes in hoursArray/minutesArray which I have inserted in the arrays, and I am getting current date as an index of arrays.

我所做的是在当天触发通知,但它不会触发下一个我每天使用应用程序的日子,因为当我在每天使用应用程序之前,当我转向后台模式并且通知响铃时,将调用触发时间方法。

What I have done is that notifications triggers in current day but it does not trigger the next day until I use app daily because when I use app daily before the trigger time method will be called when I turn to background mode and notification rings.

现在我想要通知应该在日期更改时自动触发,即使我有一天没有使用该应用程序,这些所有内容应该在 didEnterBackground ... appDelegate

Now I want the notifications should be triggered automatically when date changes, even when I don't use the application for somedays, these all stuff should be in didEnterBackground... method of appDelegate

我跟着这个答案,但它每天都在同一时间使用但我希望每天从阵列基于当前日期的不同时间数组的索引(它表示当前日期,例如今天是4月19日,小时和分钟数组的索引应该是19)。

I have followed this answer, but it is used for same time daily But I want different time daily from arrays based on Current date's index of arrays (it means the current day e.g today is 19 April the index of hours and minutes array should be 19).

这是我的数组的样子

小时数组= [9,10,11,13,14,11,17,2,15,5 ......等等]

Hour array = [9, 10, 11, 13, 14, 11, 17, 2, 15, 5.... and so on]

分钟数组= [23,00,04,58,59,12,01,33 ......等等]

Minute array = [23, 00, 04, 58, 59, 12, 01, 33,.... and so on]

方法调用 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中的方法

Significant time changes after each midnight, method in 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 ...... 当我退出我的应用程序时,通知方法将只调用一次。不是??当我甚至没有打开应用程序一周但我想收到通知时,我可以收到每日通知,如何调用方法?是每次在后台模式下调用的方法吗?

Have a look at didEnterBackground... when I quit my app the notication method will be called just once. isn't is?? and how the daily notification can I receive when I don't even open the app for a week but I want to get notifications, How method will be called? is method called every time in background mode?

有没有办法让我可以安排通知,当第一次通知完成时,第二次应该被触发,如果第二次完成那么第三个应该在后台模式中触发等等,即使我不打开应用程序一周?应根据数组中给出的当前日期和时间触发通知。

Is there any way so I can schedule the notification and and when the first notification done the second should be triggered and if second done then third should be triggered and so on in the background mode even I don't open the app for a week?? the notifications should be triggered based on current date and time given in arrays.

UPDATE
我甚至放置了刷新功能每隔10000秒刷新一次触发器,但它不起作用。

UPDATE I have even placed the refresh function which refreshes the trigger after each and every 10000 seconds but it is not working.

我还添加了 significantTimechanges ,如果有午夜的变化,但它不适用于我是如何定义所有。

I have also added significantTimechanges , if there is midnight changes but its not working to here is how I have defined that all.

推荐答案

重要注意:如果应用程序长时间处于终止状态或后台,则开发人员无法执行任何操作。所以这不能做到。

Important Note: Developer can not do anything if application is in kill state or in background for a long time. So this can not be done.

您的要求有很多问题:

1.1:您在一项服务中有多少天数据一年或?

1.1: How many days data you have in one service, for a month, for a year or ?

1.2:为什么不推送

1.3:为什么要安排本地通知每天可以同时安排多次?

1.3: Why do you want to schedule local notification on daily basis while you can schedule many at once?

1.4:有没有办法在应用程序中进行更改时未打开(被用户杀死),因此在这种情况下您无法执行任何操作。

1.4: There is no way to make changes in the app when its not opened (killed by user) so you can not do anything in that case.

现在我们可以做些什么:

2.1:考虑到您获得的是月度数据。因此,您应该通过增加日组件,在 for loop 中一次安排所有本地通知 并设置相应的时间(按日期索引从数组中)。如果用户在一个月内打开一次应用程序(他有90%的可能性)那么你的运气就好了,那么你可以向服务器请求下个月的数据并做同样的事情。

2.1: Considering you are getting monthly data. So you should schedule all the local notifications at once in for loop by increasing the day component and setting the corresponding time (from your arrays by day index). And its your luck if user opens the application once in a month (90% chances he will) then you can request to server for next month data and do the same.

2.2:为什么不在服务器端处理此问题并使用推送通知而不是 local 。服务器人员可以为每分钟配置一个cron作业,这将为用户提取所有小时和分钟,并将发送 push

2.2: Why not this is being handled at server end and use push notifications instead of local. Server guys can configure a cron job for every minute which will fetch the all hours and minutes for users and will send the push.

我建议不要使用本地但是你可以使用 2.1 (一次安排所有月份通知)。

My suggestion not to go with local but all maximum you can go with 2.1 (schedule all month notifications at once).

这篇关于每天在数组Objective C ios中给出的动态时间自动触发本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆