Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can UILocalNotification fire a custom method when App is in background mode?

Well the title is self explained. I want to create an App that can manage programmed local notifications when App is in background mode. Notifications works smoothly but I want to fire a custom method when the alert is fired.

Is it possible? Thank you.

like image 286
Jorge Ramos Avatar asked Oct 26 '11 13:10

Jorge Ramos


2 Answers

Yes it can be done. You can do somethng like this:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [NSTimer scheduledTimerWithTimeInterval:17.0 target:self selector:@selector(makeNotificationRequest:) userInfo:nil repeats:YES];
}

-(void)makeNotificationRequest:(NSTimer *)timer
{
    CLLocation *location = [[AppHelper appDelegate] mLatestLocation];
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];

#ifdef _DEBUG
    [paramDic setValue:[NSString stringWithFormat:@"77.586"] forKey:@"Lat"];
    [paramDic setValue:[NSString stringWithFormat:@"12.994"] forKey:@"long"];
#else
    [paramDic setValue:[NSString stringWithFormat:@"%f",location.coordinate.latitude] forKey:@"Lat"];
    [paramDic setValue:[NSString stringWithFormat:@"%f",location.coordinate.longitude] forKey:@"long"];
#endif

    WNetwork *mNetwork = [[WNetwork alloc] init];
    [mNetwork makeRequsetWithURL:URL_Showbeeps type:JBJsonParser paramDictionary:paramDic delegate:self];
    [mNetwork autorelease];
    NSLog(@"URL HIT%@",paramDic);
    [paramDic autorelease];
}

And to customize your action on alert you can use this:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        ;
    }
}
like image 146
iPhoneDev Avatar answered Oct 06 '22 01:10

iPhoneDev


If the app were in the foreground then you would want the - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification method of UIApplicationDelegate, however the documentation suggests that this will not be called when the app is in the background.

You can get a list of local notifications by calling scheduledLocalNotifications on your UIApplication instance - you can then poll these for their times and schedule a function to call at that time in your background mode. This won't necessarily 100% match up with when the Local Notification fires though, but I think it's as close as the App Store guidelines will let you get.

You can also present your own Local Notifications when you are in background mode by calling the presentLocalNotificationNow: method:

https://developer.apple.com/library/IOS/#documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/presentLocalNotificationNow:

so you could work around this by just presenting your own notifications and not scheduling them for the OS to deliver.

If you are trying to access Local Notifications from other applications then I don't think this is allowed.

like image 43
Benjie Avatar answered Oct 05 '22 23:10

Benjie