Background request not execute Alamofire Swift
Asked Answered
M

1

10

I'm trying to make calls in background like POST,GET to be more precise in the didReceiveRemoteNotification method, because they start to work as a push notification arrive. My problem is that all the Alamofire.request are never call in Background mode until I open the app. I have by now

Im was trying to open a session but it won't make the request work.

These is what i want to execute in background (cellphone in background)

Alamofire.Manager(configuration: configuration).request(.GET, url, parameters: nil)
                        .responseJSON { (_, _, JSON, _) in
                            //println(JSON)
                                println(JSON)
                REST OF THE CODE 

But it won't work, even if i add code below these request it works but the return of the request or even the request is not made.

Manumit answered 31/7, 2015 at 15:38 Comment(0)
T
20

While method says "background configuration", what it actually means is that the network session is configured to allow for interruptions and continuation of upload / download. What you need to do instead is to extend execution time of the application so it works for some time even in background

There is beginBackgroundTaskWithExpirationHandler: that is specifically designed to do that. When you use it, you will get few more minutes to execute whatever you need (after that limit, your application will get terminated no matter what, now your application is terminated immediately).

You can write following methods:

func beginBackgroundTask() -> UIBackgroundTaskIdentifier {
    return UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({})
}

func endBackgroundTask(taskID: UIBackgroundTaskIdentifier) {
    UIApplication.sharedApplication().endBackgroundTask(taskID)
}

When you want to use it, you just simple begin / end the task when starting / finishing the download call:

// Start task
let task = self.beginBackgroundTask()

// Do whatever you need, like download of the images
self.someBackgroundTask()

...

// End task once everything you need to do is done
self.endBackgroundTask(task)

Hope it helps!

Edit 1:

If the problem is that your download method IS NEVER called, then it means you are not sending proper data in notification payload:

For a push notification to trigger a download operation, the notification’s payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app. https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Terrill answered 31/7, 2015 at 16:54 Comment(5)
Hi, thanks for the answer , but my problem is associate a Alamofire sessions or what i need to run the requests cause those functions that you write run the code but is the request whos never return or maybe even call until i open the app. Thanks again.Manumit
See my updated answer - if the problem is that you are not getting your method called at all, above should help :)Terrill
I knew about the content-available i already have it cause is a silent notification so for example if i receive a push notification, the didReceiveRemoteNotification activate and then i want to make a request with alamofire , my main problem is that the code in didReceiveRemoteNotification works but the call or request don't do anything when the iphone is inactive/background (not open) then when I open the app it runs like should be.Manumit
maybe your question has answer here, I suggest you take a look at it: github.com/Alamofire/Alamofire/issues/194Terrill
any luck? @DanielRomeroRecurrent

© 2022 - 2024 — McMap. All rights reserved.