0

I am trying to send a receipt back to the website when a push notification has been delivered to the phone no matter what the state of the app is in (foreground, background, closed). I have attached my code and the iGotIt is just a https call to the server that doesn't get called. What do I need to do in order to get the app to do something when it receives a push notification?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    { 
     [application registerUserNotificationSettings:
       [UIUserNotificationSettings settingsForTypes:
        (UIUserNotificationTypeSound |
         UIUserNotificationTypeAlert |
         UIUserNotificationTypeBadge) categories:nil]];
       [application registerForRemoteNotifications];
    }
    else
    {
        [application registerForRemoteNotificationTypes:
         (UIUserNotificationTypeBadge |
          UIUserNotificationTypeSound |
          UIUserNotificationTypeAlert)];
    }
    NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
    NSString *messagePre = nil;
    NSString *message = nil;
    id alert = [userInfo objectForKey:@"aps"];
    if ([alert isKindOfClass:[NSString class]]) {
        messagePre = alert;
    } else if ([alert isKindOfClass:[NSDictionary class]]) {
        messagePre = [alert objectForKey:@"alert"];
    }
    NSArray *messageSplit = [messagePre componentsSeparatedByString:@"|"];
    serviceID = [messageSplit objectAtIndex:0];
    message =[messageSplit objectAtIndex:1];

    UIAlertView *alertRC = [[UIAlertView alloc]initWithTitle: @"New Road Call"
                                                     message: message
                                                    delegate: self
                                           cancelButtonTitle:nil
                                           otherButtonTitles:@"View",nil];

     [alertRC show];

    //Accept push notification when app is not open
    if (userInfo) {
        [self iGotIt];
    }

    return YES;
}

Thanks in advance for any help you can provide

zangw
  • 43,869
  • 19
  • 177
  • 214
Jim
  • 69
  • 2
  • 8

1 Answers1

1

In the case where the user opens your app by tapping a received push notification, the didFinishLaunchingWithOptions: is the right method to look at.

However, in the case where the user is already inside the app or if the app is in the background when receiving a push notification then didReceiveRemoteNotification:fetchCompletionHandler: (documentation) will be called. There's some more discussion on the subject here. Remember to set content-available=1 in the payload and tick off Remote notifications in your Capabilities for your app.

For the last case where your app has been force-quit by the user, it's unfortunately not that straightforward. This is achievable using PushKit and the subject has also been discussed here.

Community
  • 1
  • 1
Steffen D. Sommer
  • 2,896
  • 2
  • 24
  • 47