我想知道,是否有可能以某种方式“唤醒”后台任务,以快速检查网络上的某些内容。我认为这可以通过 UILocalNotification 来完成,但是,无论我尝试了什么,当应用程序在后台时,我无法让 didReceiveLocalNotification 执行任何操作。启动后,我立即按主页按钮关闭应用程序(本地通知触发有 10 秒延迟)。当应用程序处于前台时,此代码可以完美运行,并且只是坐在那里......
在应用程序委托头文件中:
UILocalNotification *localNotif;
为了测试,我设置了本地通知以在 appDelegate 启动时快速触发。
localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; // the date you want the notification to fire.
localNotif.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(@"setup the timer for 10 seconds");
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
NSLog(@"getting kicked");
if (state == UIApplicationStateInactive) {
// Application was in the background when notification was delivered.
NSLog(@"INACTIVE..");
} else {
NSLog(@"ACTIVE..");
}
}