2013-03-24 78 views
3

我正在爲iPad開發iOS應用程序。我正在使用名爲HelpShift的服務使用推送通知。我想在用戶點擊通知時運行一段代碼。它實際上在應用程序處於活動狀態時起作用,但當它處於後臺或不活動狀態時,它不起作用。這裏是我的代碼:推送通知後運行功能

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

if ([[userInfo objectForKey:@"origin"] isEqualToString:@"helpshift"]) { 

    UIApplicationState state = [application applicationState]; 

    if (state == UIApplicationStateActive) {   

     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"You were answered in HelpShift" 
                  message:@"Hello" 
                  delegate:self 
                cancelButtonTitle:@"Cancel" 
                otherButtonTitles:@"Show", nil]; 
     [alertView show]; 

    } if (state== UIApplicationStateBackground) { 

     UIViewController *vc = self.window.rootViewController; 
     [[Helpshift sharedInstance] handleNotification:userInfo withController:vc];    

     [self showHelpShift]; 

    } if (state == UIApplicationStateInactive) { 

     UIViewController *viewController = 
     [[UIStoryboard storyboardWithName:@"MainStoryboard" 
            bundle:NULL] instantiateViewControllerWithIdentifier:@"home"]; 

     [[Helpshift sharedInstance] handleNotification:userInfo withController:viewController]; 


    }   
    } 
} 


- (void) showHelpShift { 
    UIViewController *vc = self.window.rootViewController; 
    [[Helpshift sharedInstance] showSupport:vc]; 
    } 

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 

    if (buttonIndex == 1){ 
    UIViewController *vc = self.window.rootViewController; 
    [[Helpshift sharedInstance] showSupport:vc];} 
} 

因此,大家可以看到,問題是[自showHelpShift]不會被調用或者被調用提前。

+0

通知發送到傳遞給vapplicationDidLaunch消息的字典中。上述消息不在發佈時發送。 – 2013-03-24 12:28:45

+0

那麼?我有什麼編碼? – 2013-03-24 12:37:09

+0

所以你實現了推送通知,但從來沒有讀過Apple的優秀指南呢?打開「本地通知和推送通知」指南並閱讀「處理本地和遠程通知」部分。 – 2013-03-25 12:11:13

回答

2

執行application:didFinishLaunchingWithOptions:並在launchOptions字典中查找UIApplicationLaunchOptionsRemoteNotificationKey鍵。

+0

我不知道你的意思。我試過UIApplicationLaunchOptionsRemoteNotificationKey,但應用程序崩潰... – 2013-03-24 13:12:04