当应用程序在后台时使用NSNotifier来调用方法

问题描述:

我正在使用NSNotifier来创建一个闹钟,当达到时间设置时调用方法。当应用程序处于前台时,我可以成功地运行它。但是在后台时,我只能得到一个通知来呈现。当应用程序在后台时使用NSNotifier来调用方法

当应用程序在后台时是否可以调用使用NSNotifier的方法?如果是的话,我该如何做到这一点?

这里是我到目前为止的代码: -

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]; 
} 

根据文档,-application:didReceiveLocalNotification:只有被调用时,应用程序在前台。

请参阅文档UIApplicationDelegatehandling local and remote notifications