Search code examples
iosswiftalamofire

Unable to make request with Alamofire upon receiving silent push while in background


Our backend sends our app a silent push when certain events take place. Upon receiving the silent push, the app is supposed to make an API call to the server so that when the user opens the app later on, he sees updated content. The app receives the silent push notification but Alamofire doesn't make the request to our backend. The following is my implementation of the AppDelegate method which is invoked when a silent push is received (code has been redacted a bit to make it easier to read):

func application(_ application: UIApplication,     didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    self.alamoFireManager.request("someUrl/", method: .get, encoding: JSONEncoding.default).responseJSON {
        response in

        //Parse data...
        completionHandler(.newData)
    }
}

The self.alamoFireManager is set up as follows:

let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = timeout
configuration.timeoutIntervalForResource = timeout

self.alamoFireManager = Alamofire.SessionManager(configuration: configuration)

Here is where it gets strange. When I launch the app from Xcode and put it in the background and send a push from the server to the app, the app receives the push notification and makes the necessary request to the server. When I kill the app and launch the app normally on the device (not through Xcode), and trigger a push, the app never makes the request. I can't determine whether it's that the push doesn't arrive or whether it's that AF is not making the request but the push notification is configured correctly (has the content-available with a value of 1) and the corresponding delegate method is invoked in the debugger at least so I think it's that AF is not making the request. I also changed the URLSessionConfiguration to be a background session like so:

let configuration = URLSessionConfiguration.background(withIdentifier: "someIdentifier")

This change had no visible effect on the behavior of the app. What am I doing wrong? Does Alamofire not support making requests (using request()) while the app is in the background? Do I have to use NSURLSession directly? This is my first time working with any silent push-triggered background requests. Any help is appreciated, thank you.

Regards Chris


Solution

  • After posting a local notification in the didReceiveRemoteNotification method I noticed that the notification was only posted when the app was started from Xcode. When starting the app from the device directly the app never posted the local notification. This leads me to believe that my app is not actually being woken up by the push notification. This therefore has nothing to do with Alamofire at this time so I will close this question.