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];}
 }

如您所见,问题在于 [self showHelpShift] 没有被调用或被提前调用。

4

1 回答 1

2

实现application:didFinishLaunchingWithOptions:并在字典中查找UIApplicationLaunchOptionsRemoteNotificationKey键。launchOptions

于 2013-03-24T12:29:59.513 回答