Trigger local notifications automatically daily on dynamic time given in arrays Objective c ios

南楼画角 提交于 2019-11-26 20:57:40

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 data you have in one service, for a month, for a year or ?

1.2: Why not push?

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

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.

Now come to what we can do:

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: 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.

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

Just use below statement instead of NSCalendar

UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:(hours*3600)+(Mins*60)];

//Unit of time interval should be in Second

notification.repeatInterval = NSDayCalendarUnit;
[notification setAlertBody:@"This is your task time"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; 

As others have said, the better technique is to schedule all of the notifications at once. This is what I do. However, there is a system limit of 64 pending local notifications. In your case, the scheduled notification should trigger every day for 64 days. I have a more difficult problem because I need to schedule notifications every 15 minutes. So, if my users don't respond within 16 hours, they will not get further notifications (64/4 = 16). Also, note that UILocalNotification is deprecated, so you should use UNNotificationRequest. Note that the identifier in UNCalendarNotificationTrigger must be unique for each request.

    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);
        }
    }];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!