我正在使用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];
}
答案 0 :(得分:0)
根据文档,-application:didReceiveLocalNotification:
仅在应用程序位于前台时被调用。
请参阅UIApplicationDelegate和handling local and remote notifications的文档。