Sending One Signal Tags when the application terminates in iOS

66 views Asked by At

In our iOS e-commerce application, i have setup ONESIGNAL, and sending "cart_update". I am sending "cart_update" to true in the situation where the user has atleast one item inside the cart and the application reaches termination state or background state. Also i have figured out that this process somewhat takes around 7-9 seconds for the tags to be successfully sent to the ONESIGNAL servers. I have tried following and facing listed problems:

  1. Perform background fetch: in this the succession of sending tags to the ONESIGNAL server only gets executed after the user enters the app again.

  2. application will terminate: In this, it doesn't get executed at all. so to check i have setup a Timer of it once it reaches in both applicationWillTerminate & applicationEntersBackground and i noticed that the timer just runs for two seconds and disappears, where the process atleast takes 7 seconds to complete.

  3. lastly i also used BGTask, but that it also not helping in anyway.

Below is the code i send to one signal:

let AbondenTime = Int(Date().timeIntervalSince1970)
        let dict: [String: String] = [
            "cart_update": "\(AbondenCart.isAbondened)",
            "product_name": AbondenCart.productName,
            "product_image": AbondenCart.productImage,
            "update_time": "\(AbondenTime)"]
        
        if let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted),
           let nsString = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue){
            let jsonString = nsString as String
            print("ONE SIGNAL JSON STRING: \(jsonString)")
            
            OneSignal.sendTags(withJsonString: jsonString)
        }

NOTE: If i run the above code when the application is in foreground for atleast 10 seconds. It is successfully posting the tags to the ONESIGNAL servers and i can see the data appears instantly below the AUDIENCE Tab.

0

There are 0 answers