2014-09-02 60 views
1

1)我實現了下面的方法。我瞭解didReceiveRemoteNotification:fetchCompleteionHandler適用於iOS7 +。此外,它僅提供30秒來完成請求操作。ios推送通知 - 無法在後臺應用程序異步回調中從Web服務接收響應

- (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)我米無法看到警報或NSLog的輸出,除非應用程序是在前臺。當我啓動應用程序時,只有我能看到。

我在做什麼錯了?

+0

什麼是您配置的推送通知類型?它是一個徽章/聲音/警報? – 2014-09-02 08:03:01

+0

配置爲所有,但我只發送徽章,內容可用,armyID。另外,我能夠運行didReceiveRemoteNotification,並且服務器端還有一個請求也會從這裏開始。但只有在應用程序到達前臺時纔會傳遞響應。 – Whoami 2014-09-02 08:14:05

回答

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 2014-09-02 08:30:32

+0

只有在背景中爲** **時才能使用。但是,如果應用程序被終止,它將不起作用。所以,你必須用兩種不同的方法來處理通知。 – Ricky 2014-09-02 08:35:49

+0

在** didReceiveRemoteNotification **方法中添加更多信息。您必須以不同的方式處理通知,當應用程序處於後臺時不能使用AlertView。 – Ricky 2014-09-02 08:45:37

0

嘛,這是想我發現,和現在的工作。如果某人可以使用,那就很開心。

1)APNS通知支持:雖然在後臺,您無法收到通知,直到在iOS6的,但是在iOS7,該選項與API

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

2啓用),我們必須確保在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]; 

希望它可以幫助節省時間的人。

相關問題