0

I need to update orders from my app while app is in background.

Ok, I am using OneSignal, I can get message on didReceiveRemoteNotification and inside it, I call Alamofire to check on my api what I need to update.

The problem is when the code get to the point: Alamofire.request(url).responseJSON {(response) in it doesnt go inside, just when I open the app I can get the result.

I would like it to get the new data on background and notify users after updating, so they can click on the notification to see whats is new.

I read that Alamofire runs on a background thread by default, but the network request goes on Main thread.

So, I tried: this and this, both don't work.

I tried URLSessionConfiguration but I got Error code -999 cancelled. So, I added sessionManager.session.finishTasksAndInvalidate() in the end of my response. The error stops, but the code still don't go inside Alamofire request.

Some of my code - didReceiveRemoteNotification on my AppDelegate:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        if let custom = userInfo["custom"] as? NSDictionary {
            if let a = custom["a"] as? NSDictionary {
                if let update = a["update"] as? NSString {
                    if update.isEqual(to: "Pedido") {
                        let strdataPedido = PedidoDAO().getMostRecentDtPedido()

                        if self.defaults.getDownloadInicialPedido(){
                            if strdataPedido != "" {

                                //let task = self.beginBackgroundTask()
                                self.loadOrdersFromLastSyncByApi(strdataPedido)
                                //self.endBackgroundTask(task)
                            }
                        }
                    }
                }
            }
        }

loadOrdersFromLastSyncByApi function on my AppDelegate:

func loadOrdersFromLastSyncByApi(_ lastSync: String) {
        let parceiroId = defaults.getParceiroId()

        PedidoAPI().loadOrdersForLastSync(parceiroId, lastSync){ (dados) in
            if let dadosPedidoModel = dados as? [PedidoModel] {
                //do what needs to do to save new data
            }

PedidoAPI().loadOrdersForLastSync function:

func loadOrdersForLastSync(_ parceiroId: String, _ lastSync: String, _ onComplete: @escaping(Any?) -> Void) {

        let url = Test.basePath + "/api/orders/parceiro/\(parceiroId)/\(lastSync)"

        //let queue = DispatchQueue(label: "com.test.br", qos: .background, attributes: .concurrent)
        let task = self.beginBackgroundTask()

        let queue = DispatchQueue.global(qos: .background)
        queue.async {
        Alamofire.request(url).responseJSON {(response) in

            //This result its fired just when I open my app, I would like it to make everything on background
            switch (response.result) {
                //do what needs to send new data
            }
        self.endBackgroundTask(task)

Any help please?

1 Answer 1

1

Thanks for your question. So to clarify, I understood your question as the following:

You want to update users when there has been a change in their orders while the app is in background.

Follow-up Question: Is there a reason you want to make the request from the client? Also, what data is coming in from OneSignal that you couldn't just handle on your server?

Answer: You should handle any requests to a third party (Alamofire) on the server and then use our API to send a notification to the user with new info from the request response. I think that would be the best approach.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.