0
votes

I have a requirement to make an web-service call up on tapping actionable buttons from the Remote notification.

On the Remote notification, we have two buttons "Details" and "Fetch". "Fetch" is a background action (Activation Mode = UIUserNotificationActivationModeBackground) and will send some data with out launching the app.

On tapping "Fetch", i made a web-service call to send some details which i got from remote notification.

I have used NSURLSession to send those data to server.

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.applewatch.sample"];

self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

NSURLSessionUploadTask *dataTask = [self.session uploadTaskWithRequest:mutableURLRequest fromData:nil];
[dataTask resume];

This works fine when the app is in active state but not in the suspended mode. What i meant to say here is, Once we get the Remote notification and if act after 1 hour on the notification, the service request is not going through.

I suspect, the service request is not going through because the app is in suspended mode/termination mode.

Could some help me how to make service calls in the background.

Note: I have not enabled "Background Fetch" options in info.plist.

Let me know if need to enable and implement the service calls in the UIApplication delegate

application:performFetchWithCompletionHandler:

1
I think that's exactly what you need to do (BackgroundFetch)Erakk
But i want to send data immediately to server. As per Apple documentation, The system must balance your app’s need to fetch content with the needs of other apps and the system itself. After assessing that information, the system gives time to apps when there are good opportunities to do so. How to send request immediately up on tapping button actions.lallu
But you still need the BackgroundFetch. you are telling the system "i probably need to do stuff in the background" and the system will "ok, we have x apps doing the same, so i will give you Y minutes only". You are still letting the system decided how to use the resources, you only have to handle when the system says "thats enough", it will let you invalidate the task. As you said, "The system must balance (apple)", so it;s the system's responsibility to decide that, you just ask.Erakk
If your problem is actually how to send a request with a button press then i suggest you post more code :)Erakk
We can use BackgroundFetch only when the app needs any content to be downloaded in the background by specifying background fetch interval so that it can minimize the delay in presenting that content to the user.lallu

1 Answers

1
votes

I'm not being able to recall or find again where I read this information before, but try using

- uploadTaskWithRequest:fromFile:

I believe it's the only upload method that will work in background