我想知道,是否有可能以某种方式“唤醒”后台的任务,以快速检查网络上的某些内容。我认为这可以通过 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..");
}
}
用户有几个选择:#1) 他们是否想看到您的应用程序的通知。 #2) 如果您的应用程序启用了通知,他们是否想要单击您的通知来启动您的应用程序。如果他们确实接受通知并在您的应用程序处于后台时打开您的通知,application:didReceiveLocalNotification
叫做。需要明确的是,用户必须接受通知(例如滑动通知下方的滑块)...否则什么都不会被调用。
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(@"%@", notification);
}
如果您的应用程序已被终止application:didFinishLaunchingWithOptions:
叫做 -
- (BOOL)application:(UIApplication *)
application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions {
UILocalNotification *theNotification =
[launchOptions
objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSLog(@"%@", theNotification);
return YES;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)