rdivate
rdivate

Reputation: 11

How do I make UILocalNotification method keep running

I am trying to build a calendar style app that reminds people when certain events are happening the day before they happen. I am using UILocalNotifications for this. I have to restart my app if I want the notification to appear.

How can I have this code continuously run regardless if the app is still running or is closed, and display the notification on time?

I was wondering if I had to put this into the applicationDidEnterBackground method to make it work?

Currently my code looks like this

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMMM dd, yyyy"];

    NSString* path = [[NSBundle mainBundle] pathForResource:@"example"
                                                     ofType:@"txt"];
    NSString* content = [NSString stringWithContentsOfFile:path
                                                  encoding:NSUTF8StringEncoding
                                                     error:NULL];
    NSArray* allLinedStrings = [content componentsSeparatedByCharactersInSet:
                                [NSCharacterSet newlineCharacterSet]];


    NSDate *tomorrow = [today dateByAddingTimeInterval:60*60*24*1];
    NSString *tomorrowString = [dateFormatter stringFromDate:tomorrow];

    for (int i = 0; i < allLinedStrings.count; i++) {
        NSString* strsInOneLine = [allLinedStrings objectAtIndex:i];
        NSArray* singleStrs = [strsInOneLine componentsSeparatedByCharactersInSet:
                               [NSCharacterSet characterSetWithCharactersInString:@";"]];
        NSString *date = [singleStrs objectAtIndex:0];
        if ([date isEqualToString:tomorrowString]) {
            for (int j = 1; j < singleStrs.count; j+=2) {
                UILocalNotification *notification = [[UILocalNotification alloc]init];
                notification.fireDate = [NSDate dateWithTimeInterval:60*60*-24 sinceDate:tomorrow];
                notification.alertBody = [singleStrs objectAtIndex:j+1];
                notification.alertTitle = [singleStrs objectAtIndex:j];
                notification.timeZone = [NSTimeZone defaultTimeZone];
                [[UIApplication sharedApplication]scheduleLocalNotification:notification];
            }
        }
    }

    // Override point for customization after application launch.
    return YES;
}

Upvotes: 0

Views: 34

Answers (1)

picciano
picciano

Reputation: 22701

Your app code does not need to be running for your local notification to be displayed. Once your app has called scheduleLocalNotification:, the notification will display whether or not you app is running.

If you app is in the foreground, you will also want to implement application:didReceiveLocalNotification:. If you want your app to respond to being opened by the user interacting with the notification, you will want to implement application:handleActionWithIdentifier:forLocalNotification:completionHandler:

As to the question of where to put the code that schedules the notification, it should go anywhere in your app that knows the event to be scheduled. It only needs to be called once per notification. It can be scheduled far in advance.

Upvotes: 4

Related Questions