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.
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
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).
Here is how my arrays look like
Hour array = [9, 10, 11, 13, 14, 11, 17, 2, 15, 5.... and so on]
Minute array = [23, 00, 04, 58, 59, 12, 01, 33,.... and so on]
method call in appdelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self.viewController triggerAutomaticallyDaily];
[self applicationSignificantTimeChange:application];
[self refreshAlarm];
}
method inside 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];
}
Significant time changes after each midnight, method in Appdelegate
-(void)applicationSignificantTimeChange:(UIApplication *)application {
[self.viewController triggerAutomaticallyDaily];
}
Refresh Alarm method in 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];
}
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 I have even placed the refresh function which refreshes the trigger after each and every 10000 seconds but it is not working.
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.
There are many questions for your requirement:
1.1: How many
days datayou have in one service,for a month,for a yearor ?1.2: Why not
push?1.3: Why do you want to schedule
local notificationon daily basis while you can schedule many at once?1.4: There is no way to make changes in the
appwhen its not opened (killed by user) so you can not do anything in that case.Now come to what we can do:
2.1: Considering you are getting monthly data. So you should schedule all the
local notificationsat once infor loopby increasing theday componentand 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: Why not this is being handled at server end and use
push notificationsinstead oflocal. Server guys can configure a cron job for every minute which will fetch the all hours and minutes for users and will send thepush.My suggestion not to go with
localbut all maximum you can go with2.1(schedule all month notifications at once).