3

I'm developing an iOS app for iPad. I'm using Push notifications with a service called HelpShift. I'd like to run a piece of code when the users taps the notification. It actually works when the app is active, but when it's background or inactive, it doesn't work. Here is my code:

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

So as you can see, the problem is that the [self showHelpShift] doesn't get called or it gets called to early.

Grokify
  • 15,092
  • 6
  • 60
  • 81
Marti Serra Vivancos
  • 1,195
  • 5
  • 17
  • 33
  • The notification comes in the dictionary passed into thevapplicationDidLaunch message. The above message is not sent at launch. – David H Mar 24 '13 at 12:28
  • So? What do I have to code? – Marti Serra Vivancos Mar 24 '13 at 12:37
  • So you implemented push notifiations, but never read Apple's excellent guide on it? Open the "Local Notifications and Push Notifications" guide and read the section on "Handling Local and Remote Notifications". – David H Mar 25 '13 at 12:11

1 Answers1

2

Implement application:didFinishLaunchingWithOptions: and look for the UIApplicationLaunchOptionsRemoteNotificationKey key in the launchOptions dictionary.

omz
  • 53,243
  • 5
  • 129
  • 141