UILocal通知在后台 10 分钟后不提示



In didFinishLaunchingWithOptions 一个计时器循环,每隔 1 分钟调用一次函数httpRequest

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //rest of code
    NSTimer *notifyTimer = [NSTimer timerWithTimeInterval:60 target:self selector:@selector(httpRequest) userInfo:nil repeats:YES];//7200.0
    [[NSRunLoop mainRunLoop] addTimer:notifyTimer forMode:NSDefaultRunLoopMode];
    return YES;
}

按下主页按钮后,应用程序将进入后台并调用函数applicationDidEnterBackground因此后台任务正在启动。

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    __block UIBackgroundTaskIdentifier bgTask;
    UIApplication *app = [UIApplication sharedApplication];
    expirationHandler = ^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
        bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
    };
    bgTask = UIBackgroundTaskInvalid;
    bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
}

通过httpRequest函数,我每 1 分钟间隔一次从 Web 服务器获取Y,因此每秒钟触发一次UILocalNotification

-(NSString *)httpRequest {
    NSURL *url = [NSURL URLWithString:@"http://192.168.10.67/t.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *userAgent = [NSString stringWithFormat:@"bgTaskTest-IOS"];
    [request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [request setHTTPMethod:@"GET"];
    [request setTimeoutInterval:25];
    NSURLResponse *response;
    NSData *dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    NSString *stringReply = [[NSString alloc] initWithData:dataReply encoding:NSASCIIStringEncoding];
    if ([stringReply isEqualToString:@"Y"]) {
        [self showLocalNotification:nil]; //calling UILocalNotification
    } else {
        NSLog(@"%@",stringReply);
    }
    return stringReply;
}

函数showLocalNotification根据函数的响应每 1 分钟调用一次httpRequest

-(void)showLocalNotification {
    NSString *msg = @"test message";
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    UILocalNotification *_localNotification = [[UILocalNotification alloc]init];
    _localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
    _localNotification.timeZone = [NSTimeZone defaultTimeZone];
    _localNotification.alertBody = msg;
    _localNotification.soundName = UILocalNotificationDefaultSoundName;
    _localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber]+1;
    [[UIApplication sharedApplication] scheduleLocalNotification:_localNotification];
    //[[UIApplication sharedApplication] presentLocalNotificationNow:_localNotification];
}

一切正常,当应用程序在后台运行时,每 1 个 munite 后通知提示。

但我的问题是后台任务的生命周期是 10 分钟,所以 10 分钟后没有通知提示。出于这个原因,我在beginBackgroundTaskWithExpirationHandler再次启动后台任务,但我的应用程序在重新启动后台任务时终止。

当应用程序在后台运行时,我无法使用通知超过 10 分钟。

请有人帮助我。

没有

办法(在应用商店指南中)在后台运行任意代码超过十分钟(正如您已经注意到的那样)。

10 分钟后,您的应用将被暂停。有几种方法可以解决此问题,注册其他背景模式(例如背景音频,连续播放无声声音文件)或后台voip或后台位置服务。

这些黑客解决方法将使您的应用程序保持未暂停状态,但是您的应用程序将不会获得商店的批准。

在 iOS7 中,在后台运行代码有一些进步,但是没有什么可以做你想要的。

因此,如果这是一个供你自己使用的应用程序,请使用私有API或我上面建议的方法,但是如果你想在商店里得到这个应用程序,恐怕你不走运。

最新更新