简体   繁体   中英

Using NSNotifier to call a method when app is in the background

I am using the NSNotifier to create an Alarm Clock that calls a method when the time set is reached. I can get this to work successfully when the App is in the foreground. But when in the background, I can only get a notification to be presented.

Is it possible to call a method using the NSNotifier, when the app is in the background? If so how do I accomplish this?

Here is my code so far:-

appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ProcessDidComplete:) name:@"ProcessDidComplete" object:nil];
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ProcessDidComplete" object:nil];
}

- (void) ProcessDidComplete:(NSNotification *)pNotification
{
    NSString *processData;
    processData = @"Processing Data";
}

viewcontroller.m

-(void)addLocalNotification
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.repeatInterval = NSMinuteCalendarUnit;
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
    notification.alertBody = @"This is local notification!";
    notification.timeZone = [NSTimeZone defaultTimeZone];

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

According to documentation, -application:didReceiveLocalNotification: only gets called when the application is in the foreground.

See documentation for UIApplicationDelegate and handling local and remote notifications .

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