2014-09-02 1 views
1

1) 아래 메소드를 구현했습니다. didReceiveRemoteNotification을 이해했습니다 : iOS7 +에서 fetchCompleteionHandler가 작동합니다. 또한 요청 작업을 완료하는 데 30 초 밖에 걸리지 않습니다.ios 푸시 알림 - 백그라운드에서 앱을 사용하는 동안 비동기 콜백에서 웹 서비스의 응답을받을 수 없습니다.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler 
{ 
    // Invoke Asynchronous request to get additional data. 
    [NSURLConnection sendAsynchronousRequest:theRequest 
             queue:[NSOperationQueue mainQueue] // created at class init 
          completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){ 
     // Handling Response Data here. 
    // DISPLAY ALERT HERE. 
    } 

    } 

질문 :

1) I 응용 프로그램이 전경에 있지 않으면 경고 또는 NSLog 출력을 볼 수없는 해요. 내가 응용 프로그램을 시작하면 볼 수 있습니다.

내가 뭘 잘못하고 있니?

+0

구성한 푸시 알림 유형은 무엇입니까? Badge/Sound/Alert입니까? –

+0

모두 구성되었지만 배지, 컨텐츠 사용 가능, armyID 만 전송합니다. 또한 didReceiveRemoteNotification을 실행하고 서버 측에서 요청을 처리 할 수 ​​있습니다. 응용 프로그램이 포 그라운드 상태가되면 응답 만 전달됩니다. – Whoami

답변

0

당신은 2 가지 방법에 푸시 알림을 처리해야합니다 : -

//Receive Push Notification when the app is active in foreground or background 
- (void)application:(UIApplication *)application 
      didReceiveRemoteNotification:(NSDictionary *)userInfo { 

    if(userInfo){ 
    //TODO: Handle the userInfo here 

    UIApplicationState state = [[UIApplication sharedApplication] applicationState]; 

    if (state == UIApplicationStateActive){ 
     //On Foreground: May be Use AlertView 
    }else{ 
     //On Background: May be Use LocationNotification 
    } 

    } 

} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    //Get the push notification when app is not open 
    NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; 

    if(remoteNotif){ 
     [self handleRemoteNotification:application userInfo:remoteNotif]; 
    } 

    return YES; 
} 

-(void)handleRemoteNotification:(UIApplication*)application userInfo:(NSDictionary*)userInfo{ 

    if(userInfo){ 
     //TODO: Handle the userInfo here 
    } 
} 
+0

응용 프로그램이 백그라운드에있는 동안 didReceiveRemoteNotification이 작동하지 않습니까? – Whoami

+0

백그라운드에서 ** 활성 ** 인 경우에만 작동합니다. 하지만 앱이 종료되면 작동하지 않습니다. 따라서 두 가지 방법으로 알림을 처리해야합니다. – Ricky

+0

** didReceiveRemoteNotification ** 메소드에 대한 자세한 정보를 추가하십시오. 다르게 알림을 처리해야합니다. 앱이 백그라운드 일 때 AlertView를 사용할 수 없습니다. – Ricky

0

음, 이것은 내가 발견하고자이며, 그것은 작동합니다. 누군가에게 유용하면 행복합니다. 우리는에서 확인해야) 백그라운드에서, 당신은 iOS6의에서까지 알림을 수신 할 수 있지만, 그러나 iOS7에에, 옵션이 API

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler 

2 사용할 수 있습니다 :

1) 알림 지원을 APNS PLIST 파일,

Required background Modes -> "App downloads content in response to push Notification". 

3) 확인은 반드시 귀하의 페이로드 메시지에는 다음이 포함

'content-available' key with value 1. 

4) 콜백 [즉 didRemoteNotification에 관련된 네트워크 호출하려면 다음 fetchCompletionHandler을

use NSURLSession rather than NSURLConnection. 

내 경우에는 내가
[[session dataTaskWithRequest:theRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
{ 
    NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; 

    NSLog (@" Result in String. %@ ", strData); 

}] resume]; 

그것이 사람에게 시간을 절약하는 데 도움이 희망 사용.