UILocalNotification wird nach 10 Minuten im Hintergrund nicht angezeigt

ImdidFinishLaunchingWithOptions eine Timer-Schleife, die eine Funktion aufrufthttpRequest alle 1 Minute.

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

Nach dem Drücken der Home-Taste geht die Anwendung in den Hintergrund und ruft die Funktion aufapplicationDidEnterBackground Eine Hintergrundaufgabe wird gestartet.

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

}

DurchhttpRequest Funktion bekomme ichY vom Webserver alle 1 Minute so aUILocalNotification Brände nach jeder Sekunde.

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

FunktionshowLocalNotification ruft alle 1 Minute an, basierend auf der Antwort vonhttpRequest Funktion.

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

}

Alles ist in Ordnung, die Benachrichtigung erfolgt nach jeweils 1 Munite, wenn sich die Anwendung im Hintergrund befindet.

Mein Problem ist jedoch, dass die Lebensdauer von Background Task 10 Minuten beträgt. Nach 10 Minuten wird keine Benachrichtigung mehr angezeigt. Aus diesem Grund starte ich die Hintergrundaufgabe wieder inbeginBackgroundTaskWithExpirationHandler aber meine anwendung tötet zu diesem zeitpunkt den neustart der hintergrundaufgabe.

Ich konnte die Benachrichtigung nicht länger als 10 Minuten verwenden, wenn sich die Anwendung im Hintergrund befindet.

Bitte helfen Sie mir jemand.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage