可以UILocalNotification被用来唤醒一个任务,那就是在后台 [英] Can UILocalNotification be used to wake up a task that is in the background

查看:102
本文介绍了可以UILocalNotification被用来唤醒一个任务,那就是在后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,是否有可能以某种方式唤醒一个任务是在后台,要迅速检查网络上的东西。我认为这可能与UILocalNotification来完成,但是,无论是我试过了,我不能让didReceiveLocalNotification做任何事情时,应用程序在后台..开机后,我立刻被$ p $关闭应用程序pssing Home键(有本地​​通知,10秒延迟火)。这code完美的作品时,应用程序在前台,而只是一种坐在那里......

I would like to know, if it is possible to somehow "wake up" a task that is in the background, to quickly check something on the network.. I think that this could be done with UILocalNotification, however, no matter what I tried, I could not get the didReceiveLocalNotification to do ANYTHING when the app is in the background.. After starting up, I immediately close the app by pressing the Home button (there is a 10 second delay for local notification to fire). This code works PERFECTLY when the app is in the foreground, and just kind of sitting there...

在应用程序委托头文件:

In app delegate header file:

 UILocalNotification *localNotif;

有关测试,我设置了本地通知在启动的appDelegate迅速激发。

For testing, I set up local notification to fire quickly in the appDelegate startup.

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)如果通知你的应用程序启用,做自己想做的点击您的通知,启动应用程序。如果他们接受的通知,并打开您的通知,而您的应用程序在后台,应用程序:didReceiveLocalNotification 被调用。需要明确的是,用户必须接受通知(如滑动通知下方的滑块)...否则NOTHING被调用。

The user has a couple of choices: #1) Do they want to see a notification for your app. #2) If notifications are enabled for your app, do they want to click on your notification to launch your app. If they do accept notifications and open your notification while your app is in the background, application:didReceiveLocalNotification is called. To be clear, the user has to accept the notification (such as sliding the slider underneath the notification)... otherwise NOTHING is called.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"%@", notification);
}

如果您的应用程序已终止的application:didFinishLaunchingWithOptions:叫做 -

If your app has been terminated application:didFinishLaunchingWithOptions: is called -

- (BOOL)application:(UIApplication *) 
application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions {
    UILocalNotification *theNotification = 
      [launchOptions 
        objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    NSLog(@"%@", theNotification);

    return YES;
}

这篇关于可以UILocalNotification被用来唤醒一个任务,那就是在后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆