0

我正在制作一个在应用关闭后发送推送通知的应用。我正在使用 AFNetworking 与我的服务器通信。这是我的 AFNetworking 功能。

-(void)sendPushNotification:(NSMutableDictionary *)params onCompletion:(JSONResponseBlock)completionBlock{
    NSLog(@"%@%@",kAPIHost,kAPIPush);
    NSMutableURLRequest *apiRequest = [self multipartFormRequestWithMethod:@"POST" path:kAPIPush parameters:params constructingBodyWithBlock:^(id <AFMultipartFormData>formData){
        //TODO: attach file if needed
    }];
    NSLog(@"Till here");
    AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:apiRequest];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
        //success !
        NSLog(@"SUCCESSSS!");
        completionBlock(responseObject);
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){
        //Failure
        NSLog(@"FAILUREE!");
        completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
    }];
    NSLog(@"tille here 2");
    [operation start];
    NSLog(@"till here 3");
}

然后为了发送推送通知,我有以下代码。

- (IBAction)changeEnglish:(id)sender {
    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSString *alertCancel = NSLocalizedString(@"Home_alertCancel", nil);
    NSString *message = @"  Do you wish to change the language? The application will shut down after this. Next up you can restart it again.";

    BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Change language" message:message];

    [alert setCancelButtonWithTitle:alertCancel block:nil];
    [alert setDestructiveButtonWithTitle:@"Ok" block:^{
        [self sendPush];
        exit(0);
    }];
}
-(void)sendPush{
    [[API sharedInstance]sendPushNotification:NULL onCompletion:^(NSDictionary *json){
        //completion
        if(![json objectForKey:@"error"]){
            NSLog(@"notification send");
        }else {
            NSLog(@"Cannot connect to the server");
        }
    }];

}

首先,当我在浏览器中执行推送通知(在浏览器中输入链接并按回车键)时,它工作正常。但是当我想在代码中执行时。它给出了以下日志。

2012-12-12 13:44:43.695 doktersApp[666:907] http://linkexample/simplepush.php
2012-12-12 13:44:43.702 doktersApp[666:907] till here
2012-12-12 13:44:43.705 doktersApp[666:907] till here2
2012-12-12 13:44:43.706 doktersApp[666:907] till here3
4

1 回答 1

0

你想在出口做吗?那么问题是请求是异步的。操作系统不会等待它并终止应用程序,并且操作没有完成,但在您调用 start 后立即被终止。

一种解决方案是将其包装在 backgroundTask 中,然后告诉 ios 退出后您需要更多时间。

例子:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    //begin
    UIBackgroundTaskIdentifier identifier = [application beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"expired");
    }];
    assert(identifier != UIBackgroundTaskInvalid);

    //this simulates a request
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        //THIS is your completion handler
        NSLog(@"done");
        [application endBackgroundTask:identifier];
    });
}

在您的具体情况下:(这显然未经测试,以上已测试)

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    //begin
    UIBackgroundTaskIdentifier identifier = [application beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"expired");
    }];
    assert(identifier != UIBackgroundTaskInvalid);

    [[API sharedInstance]sendPushNotification:NULL onCompletion:^(NSDictionary *json){
        //completion
        if(![json objectForKey:@"error"]){
            NSLog(@"notification send");
        }else {
            NSLog(@"Cannot connect to the server");
        }
        NSLog(@"done");
        [application endBackgroundTask:identifier];
    }];
}
于 2012-12-12T13:17:35.043 回答