2012-12-12 158 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"); 
} 

然後發送pushnotification我有以下代碼。

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

你想在出口上做? 那麼問題是請求是異步的。操作系統並沒有等待它,並殺死了應用程序,操作沒有完成,但是在你調用啓動後立即死亡。

一種解決方案是將其包裝在背景任務中,並告訴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]; 
    }]; 
} 
+0

我怎樣才能解決這個問題? – Steaphann

+0

對不起,我在這裏被打斷:)現在編輯我的答案 –

+0

好的非常感謝你 – Steaphann