Push notification showing before background refresh finished (using Push with background refresh)

81 views Asked by At

I am trying to set up my app so when a push is received it first fetches the new messages before showing the push notification. In my push notification I have content-available = 1 set and also the switches enabled in the Plist. I have implemented:

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

    if([[userInfo objectForKey:@"aps"] objectForKey:@"content-available"]){

        NSLog(@"Doing the background refresh");
        UINavigationController *navigationController=(UINavigationController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];

        MYViewController *MYViewController = (MYViewController *)[[navigationController viewControllers] objectAtIndex:1];

        [myViewController.currentUser refreshMessagesArrayWithCompletionHandler:^(BOOL successful, BOOL newMiaos) {

            NSLog(@"messages refreshed the array now has %lu messages",(unsigned long)[myViewController.currentUser.messages count]);
            handler(UIBackgroundFetchResultNewData);

        }];




    }
}

The app push will come in before the background finish has finished or during the refresh. Surely it should come in after. Am I dealing with this wrong? Do I need to send a silent push notification instead and show a local notification after the completion block?

1

There are 1 answers

8
l0gg3r On BEST ANSWER

I had an issue similar as yours,
I resolved it, by sending a push notification like this

{
        "alert": null,
        "content-available": 1,
        "data-id" : "some_data_id"
}

In this case push notification will not be displayed, so in background fetch you can perform fetching and trigger a local notification.