UILocalNotification dosen't prompts after 10 m

2019-02-15 22:48发布

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;
}

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

}

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;
}

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.

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.

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

Please anybody help me.

1条回答
The star\"
2楼-- · 2019-02-15 23:40

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).

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.

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

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

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.

查看更多
登录 后发表回答