推送通知后运行一个功能

时间:2013-03-24 12:26:20

标签: ios ipad push-notification apple-push-notifications helpshift

我正在开发适用于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];}
 }

正如您所看到的,问题是[self showHelpShift]没有被调用或者它被提前调用。

1 个答案:

答案 0 :(得分:2)

实施application:didFinishLaunchingWithOptions:并在UIApplicationLaunchOptionsRemoteNotificationKey词典中查找launchOptions键。