Push Notifications in Background, How to use URLSession?

From a strategy perspective I use BGAppRefreshTask and BGProcessingTasks, successfully implementing them for events triggered within the client application.

But there are certain events that are best triggered from the host, such as when we deploy a new set of vendor or price lists. We are trying to implement this background process using Push Notifications.

We have successfully implemented sending APNS notifications from the host and receive this .json message in.
application: didReceiveRemoteNotification: fetchCompletionHandler:

using this code:

-	(void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler
{
	NSLog(@"userInfo - %@", userInfo);
	switch (application.applicationState)
	{
		case UIApplicationStateBackground:
		{
			//Check to see if this is a configs file update background notification
			NSArray *dictKeys	=	[userInfo allKeys];
			BOOL foundConfigs	=	NO;
			for (NSString *dk in dictKeys) if ([dk isEqualToString:kCONFIGSVERSION]) foundConfigs = YES;
			if (foundConfigs)
			{
				id configVersion		=	[userInfo objectForKey:kCONFIGSVERSION];
				NSLog(@"AAAppDelegate configVersion - %@", configVersion);
				if ([ServerUtilities deviceAssociatedWithAccount]) [self.bkSessionManager retrieveAndInstallUpdatedConfigFilesWithVersion:[configVersion integerValue]];
			}
			NSLog(@"AAAppDelegate remote notification handler");
			completionHandler(UIBackgroundFetchResultNewData);
		}
			break;
		case UIApplicationStateActive:
			NSLog(@"AAAppDelegate application is active");
			completionHandler(UIBackgroundFetchResultNoData);
			break;
		case UIApplicationStateInactive:
			NSLog(@"AAAppDelegate application is inactive");
			completionHandler(UIBackgroundFetchResultNoData);
			break;
		default:
			break;
	}

In the Xcode simulator running on an iPhone XS, we place the application in background and then send the APNS message from the host, receiving this .json message in the userInfo dictionary:

userInfo - {
    "app_version" = "0.1";
    aps =     {
        "content-available" = 1;
    };
    "configs_version" = 1;
    "dev_os" = 13;
    "os_type" = ios;
}

Receipt of this notification then triggers the client to initiate URLSession and retrieve the appropriate files from the host in order to update values on the client. As we are seeing inconsistent behavior we suspect that we are not implementing the URLSession properly.

When setting up the URLSession should it be configured using:

NSURLSessionConfiguration defaultSessionConfiguration

or

NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:

?

This is important to know since using backgroundSession requires us to set up the sessions as download tasks and the defaultSession as data tasks. I have tried various combinations so far without success.

Any leads here will help...

Answered by DTS Engineer in 766821022

When setting up the URLSession should it be configured using [a standard or background session]?

That depends. There’s a trade-off between latency and reliability:

  • A standard session will get you the results quickly but it only works while you have background execution time.

  • A background session doesn’t need background execution time — the work is handed off to a system process that does it on your behalf — but the response may be delayed quite a bit, possibly until that evening when the device is on Wi-Fi and mains power.

If you use a standard session you need to extend your runtime while the request is in flight. For advice on how to do that, see UIApplication Background Task Notes for more on this. This puts significant limits on how much background execution time you have; if the request is large or the network is slow, you may run out.

You can also implement a hybrid approach, putting the request in a standard session initially and then, if you run out of time, cancelling that and retrying in a background session.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

When setting up the URLSession should it be configured using [a standard or background session]?

That depends. There’s a trade-off between latency and reliability:

  • A standard session will get you the results quickly but it only works while you have background execution time.

  • A background session doesn’t need background execution time — the work is handed off to a system process that does it on your behalf — but the response may be delayed quite a bit, possibly until that evening when the device is on Wi-Fi and mains power.

If you use a standard session you need to extend your runtime while the request is in flight. For advice on how to do that, see UIApplication Background Task Notes for more on this. This puts significant limits on how much background execution time you have; if the request is large or the network is slow, you may run out.

You can also implement a hybrid approach, putting the request in a standard session initially and then, if you run out of time, cancelling that and retrying in a background session.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Push Notifications in Background, How to use URLSession?
 
 
Q