UILocalNotification在后台10分钟后不会提示 [英] UILocalNotification dosen't prompts after 10 mins in background

查看:86
本文介绍了UILocalNotification在后台10分钟后不会提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In didFinishLaunchingWithOptions a timer loop calling a function httpRequest every 1 minute interval.

- (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 所以后台任务正在启动。

After pressing home button application is going to background and calling function applicationDidEnterBackground so a background task is starting.

- (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 function我每隔1分钟就从网络服务器发出 Y ,这样每隔几秒就会触发一次 UILocalNotification

By httpRequest function I am geting Y from web server after every 1 minute interval so a UILocalNotification fires after every seconds.

-(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 是根据 httpRequest 函数的响应每1分钟调用一次。

Function showLocalNotification is calling after every 1 minute based on response of httpRequest function.

-(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];

}

一切都是正确的,每申请一次后通知会提示是在后台。

Everything is right, notification prompts after every 1 munite when application is in background.

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

But my problem is Background Task's life time is 10 mins, so after 10 mins no notification prompts. For this reason I am starting Background task again in beginBackgroundTaskWithExpirationHandler but my application kill at this time of restarting background task.

当应用程序处于后台时,我无法使用超过10分钟的通知。

I couldn't able to use notification more than 10 mins when application is in background.

请有人帮助我。

推荐答案

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

There is no way (within the app store guidelines) to run arbitrary code in the background for longer than ten minutes (as you have noticed).

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

After 10 minutes your app will be suspended. There is a couple of ways around this, registering for other background modes (such as background audio, playing a silent sound file continuously) or background voip or background location services.

这些hacky工作将使您的应用程序取消暂停,但是您的应用程序将不会获得商店批准。

These hacky work around will keep your application unsuspended however your application will not get approved for the store.

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

in iOS7 there are advances to running code in the background, however nothing that will do what you want.

因此,如果这是一个供你自己使用的应用程序,请使用私有API或我上面建议的方法,但是如果你想在商店买这个应用程序,我担心你的运气不好。

So if this is an app for your own use, use private API's or the method I suggested above, however if you want to get this app on the store, I'm afraid your out of luck.

这篇关于UILocalNotification在后台10分钟后不会提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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