1

I am sending the following APN

{"aps":{"alert":{"body":"Hello sir","action-loc-key":"Caption of the second Button"},"badge":1,"sound":"default","content-available":1},"Key1":"Value1","Key2":"Value2"}

As you see I am setting "content-available":1 as part of payload

When my application is running in foreground, everything work fine. But when my application is running in background and APN Notification comes. Notification display on screen correctly. When I click the icon , it does not call didReceiveRemoteNotification or didFinishLaunchingWithOptions

I do not know what I am missing.

My code is below:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
    navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem;
    splitViewController.delegate = self;


    // Register for Remote Push Notification 
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *mySetings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:mySetings];
        [application registerForRemoteNotifications];
        NSLog(@"didFinishLaunchingWithOptions called");
    }



    //Accept push Notification when app is not open
    NSLog(@"Accept push Notification when app is not open");
    NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (remoteNotif)
    {
        NSLog(@"Accept push Notification when app is not open if stat ");
        [self processRemoteNotificationApplicationStateActive:remoteNotif];
    }
    return YES;
}




-(void) sendDeviceToken:(NSString *) DeviceToken
{
    NSLog(@"sendDeviceToken called");

}


-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *deviceTokenString = [[[NSString stringWithFormat:@"%@", deviceToken]      //convert NSData to NSString with stringWithFormat
                                    stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]stringByReplacingOccurrencesOfString:@" " withString:@""]; // trim the "<>" then remove the spaces
    NSLog(@"deviceTokenString : %@", deviceTokenString);
    [self sendDeviceToken:deviceTokenString];
    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken called");
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"Error in registration. Error: %@", error);
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
     NSLog(@"didReceiveRemoteNotification called");

    if ( application.applicationState == UIApplicationStateActive)
    {
        //App is already in the foreground
        NSLog(@"App is already in the foreground");
        [self processRemoteNotificationApplicationStateActive:userInfo];
    }
    else
    {
        NSLog(@"App was just brought from background to foreground");
        //App was just brought from background to foreground
        [self processRemoteNotificationApplicationStateActive:userInfo];
    }

}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
     NSLog(@"didReceiveRemoteNotification fetchCompletionHandler called");

    if ( application.applicationState == UIApplicationStateActive)
    {
        //App is already in the foreground
        NSLog(@"App is already in the foreground");
        [self processRemoteNotificationApplicationStateActive:userInfo];
    }
    else
    {
        NSLog(@"App was just brought from background to foreground");
        //App was just brought from background to foreground
        [self processRemoteNotificationApplicationStateActive:userInfo];
    }

    completionHandler(UIBackgroundFetchResultNewData);
}



-(void)processRemoteNotificationApplicationStateActive:(NSDictionary *)userInfo
{


    [self.myDetailViewController performAPNUpdate];

}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    NSLog(@"applicationWillResignActive called");

}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    NSLog(@"applicationDidEnterBackground called");
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
     NSLog(@"applicationWillEnterForeground called");
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    NSLog(@"applicationDidBecomeActive called");
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
     NSLog(@"applicationWillTerminate called");
}

#pragma mark - Split view

- (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController {
    if ([secondaryViewController isKindOfClass:[UINavigationController class]] && [[(UINavigationController *)secondaryViewController topViewController] isKindOfClass:[DetailViewController class]] && ([(DetailViewController *)[(UINavigationController *)secondaryViewController topViewController] detailItem] == nil)) {
        // Return YES to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
        return YES;
    } else {
        return NO;
    }
}

2 Answers 2

4

When you open the app by tapping on the app icon you do not get any information about the push notification. Apple expects you to sync with your own server in that case.

Thus didReceiveRemoteNotification will never be called. If your app is terminated, didFinishLaunchingWithOptions will be called but you will not get any information about the push notification.

This behaviour does not make much sense to me personally, but to work around it we have created a notification syncing webservice whenever the app is opened.

1
  • Glad to help. You can accept my answer by clicking the tick and upvoting it, if it solved your question.
    – vin25
    Feb 19, 2015 at 7:37
1

When you app is running in background and the push with content-available is received, the callback method application:didReceiveRemoteNotification:fetchCompletionHandler: will be called immediately.

When you tap on the alert and the app is opened at this point, no other callback methods will be called. That is an expected behaviour since application:didReceiveRemoteNotification:fetchCompletionHandler: was already called.

It will be weird if your log did not show application:didReceiveRemoteNotification:fetchCompletionHandler: at all.

Also, make sure UIBackgroundModes include remote-notification for silent push.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.