iOS/Obj-C - didReceiveRemoteNotification statements not firing?

Viewed 337

I'm trying to post a local notification in order to change the badge number on my tab bar item when a remote notification is received. If a notification is received when the app is open, my else statement below fires perfectly. However, if the app is in the background, my first two if statements never seem to fire?

AppDelegate.m

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{

    if(application.applicationState == UIApplicationStateInactive) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

        NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background");

        completionHandler(UIBackgroundFetchResultNewData);
    }
    else if (application.applicationState == UIApplicationStateBackground) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

        NSLog(@"application Background - notification has arrived when app was in background");
        NSString* contentAvailable = [NSString stringWithFormat:@"%@", [[userInfo valueForKey:@"aps"] valueForKey:@"content-available"]];

        if([contentAvailable isEqualToString:@"1"]) {

            NSLog(@"content-available is equal to 1");
            completionHandler(UIBackgroundFetchResultNewData);
        }
    }
    else {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

        NSLog(@"application Active - notication has arrived while app was opened");

        completionHandler(UIBackgroundFetchResultNewData);
    }
}

OR: I also tried the below code, and same issue...

-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

        NSLog(@"App notification received!");
        // do stuff when app is active

    }else{

        static int i=1;
        [UIApplication sharedApplication].applicationIconBadgeNumber = i++;


        NSLog(@"App notification received background!");
    }
}

THE FIX (for those using Drupal): Add the following to push_notifications.admin.inc (Drupal Push Notifications file)**

$payload['content-available'] = 1;
1 Answers
Related