4

我想知道,是否有可能以某种方式“唤醒”后台任务,以快速检查网络上的某些内容。我认为这可以通过 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..");

}

}

4

1 回答 1

5

用户有几个选择:#1)他们是否希望看到您的应用程序的通知。#2) 如果您的应用启用了通知,他们是否希望点击您的通知来启动您的应用。如果他们确实接受通知并在您的应用程序处于后台时打开您的通知, application:didReceiveLocalNotification则会调用。需要明确的是,用户必须接受通知(例如在通知下方滑动滑块)......否则会调用NOTHING。

- (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;
}
于 2012-11-09T03:51:20.453 回答