简体   繁体   中英

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.

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) {
        ;
    }
}

No ..no custom methods are defined while app is running in background..Once an notification is fired we can;t change the alert message also. But nice when we show the alert message then by clicking the yes button to the alert take you to the one method called app is Running in Background which is in AppDelegate.m file

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM