Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Local Notification repeat interval to custom time interval?

I am making an iPhone app, which has a requirement of Local Notifications.

In local notifications there is repeatInterval property where we can put the unit repeat intervals for mintute, hour, day,week,year, etc.

I want that repeat interval should be 4 hours.

So every 4 hours the local notification comes.

I dont want the user to set seperate notifications for each.

I want the user to be able to set repeatInterval as 4 hours.

How do I do that?

like image 909
Parth Bhatt Avatar asked Dec 06 '10 06:12

Parth Bhatt


3 Answers

Got the answer, it is as straight as it gets.

You cannot create custom repeat intervals.

You have to use on NSCalendarUnit's in-built Unit Time Intervals.

I tried all the above solutions and even tried other stuffs, but neither of them worked.

I have invested ample time in finding out that there is no way we can get it to work for custom time intervals.

like image 69
Parth Bhatt Avatar answered Sep 17 '22 19:09

Parth Bhatt


The repeat interval is just an enum that has a bit-map constant, so there is no way to have custom repeatIntervals, just every minute, every second, every week, etc.

That being said, there is no reason your user should have to set each one. If you let them set two values in your user interface, something like "Frequency unit: Yearly/Monthly/Weekly/Hourly" and "Every ____ years/months/weeks/hours" then you can automatically generate the appropriate notifications by setting the appropriate fire date without a repeat.

UILocalNotification *locNot = [[UILocalNotification alloc] init];
NSDate *now = [NSDate date];
NSInterval interval;
switch( freqFlag ) {     // Where freqFlag is NSHourCalendarUnit for example
    case NSHourCalendarUnit:
        interval = 60 * 60;  // One hour in seconds
        break;
    case NSDayCalendarUnit:
        interval = 24 * 60 * 60; // One day in seconds
        break;
}
if( every == 1 ) {
    locNot.fireDate = [NSDate dateWithTimeInterval: interval fromDate: now];
    locNot.repeatInterval = freqFlag;
    [[UIApplication sharedApplication] scheduleLocalNotification: locNot];
} else {
    for( int i = 1; i <= repeatCountDesired; ++i ) {
        locNot.fireDate = [NSDate dateWithTimeInterval: interval*i fromDate: now];
        [[UIApplication sharedApplication] scheduleLocalNotification: locNot];
    }
}
[locNot release];
like image 29
LavaSlider Avatar answered Sep 19 '22 19:09

LavaSlider


I have one idea how to do this, i've implemented this in my project

First, create local notification with fire date (for example, every minute). Next step - fill user info with unique id for this notification (if you want to remove it in future) and your custom period like this:

-(void) createLocalRepeatedNotificationWithId: (NSString*) Id
{

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    NSTimeInterval your_custom_fire_interval = 60; // interval in seconds
    NSDate *remindDate = [[NSDate date] dateByAddingTimeInterval:your_custom_fire_interval];
    localNotification.fireDate = remindDate;
    localNotification.userInfo = @{@"uid":Id, @"period": [NSNumber numberWithInteger:your_custom_fire_interval]};
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

After that, implement -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification in your AppDelegate:

  1. Fetch your custom period from user info.
  2. Change fire date for next period
  3. Just add it into the sheluded notificatiots again!

    NSInteger period = [[notification.userInfo objectForKey:@"period"] integerValue]; //1
    NSTimeInterval t= 10 * period;
    notification.fireDate =[[NSDate date] dateByAddingTimeInterval:t]; //2 
     [[UIApplication sharedApplication] scheduleLocalNotification:notification]; //3
    

if you want to remove this notification, do

UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
    NSDictionary *userInfoCurrent = oneEvent.userInfo;
    NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"id"]];
    if ([uid isEqualToString:notification_id_to_remove])
    {
        //Cancelling local notification
        [app cancelLocalNotification:oneEvent];
        break;
    }
}

Very important!

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification do not called, when you at background. So, you must setup long-running background task, where you will create notification again.

like image 42
Doro Avatar answered Sep 17 '22 19:09

Doro