2012-12-12 8 views
0

앱을 종료 한 후 푸시 알림을 전송하는 앱을 만들고 있습니다. 내 서버와 통신하기 위해 AFNetworking을 사용하고 있습니다. 여기 AFNetworking 기능이 있습니다.POST 요청이 서버를 통해 성공적으로 전송되지 않았습니다.

-(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"); 
     } 
    }]; 

} 

, 내 브라우저에서 pushnotification을 실행할 때 (히트 입력 도중에 브라우저에서 링크를 입력) 그것을 잘 작동합니다. 하지만 코드에서 실행하고 싶을 때. 다음 로그를 제공합니다.

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 

답변

0

종료 할 때 수행 하시겠습니까? 그러면 요청은 비동기 적입니다. OS는 그것을 기다리지 않고 앱을 죽이고 작업이 끝나지 않지만 시작한 후에 바로 죽습니다.

하나의 해결 방법은 backgroundTask에 랩핑하고 종료 후 더 많은 시간이 필요하다는 것입니다.

예 : 당신의 구체적인 경우에

- (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]; 
    }]; 
} 
+0

그리고 내가 그것을 어떻게 해결할 수 있습니다 (이 확실히 검증되지 않은, 위의 테스트)? – Steaphann

+0

죄송합니다. 여기에 방해가되었습니다. :) 아플 편집 지금 내 답변을 –

+0

고맙습니다. – Steaphann

관련 문제