我正在处理推送通知数据,然后在推送通知自定义数据的基础上调用API。当应用处于活动和后台状态时,这将正常工作。
但是当应用程序未运行然后点击通知时,我能够从自定义日期获取自定义数据但是,API未被调用且应用程序卡住了。
我在iOS 10和11中检查过,但没有工作
处理推送是这样的。
的AppDelegate
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
redirectToScreen(notificaiton: userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
//Notify through Notification center
func redirectToScreen(notificaiton: [AnyHashable: Any]) {
let dictPayload = notificaiton as NSDictionary
print(dictPayload)
if let type = dictPayload.value(forKey: "type") as? String {
var dict = ["type" : type]
NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "handlePush"), object: dict)
}
}
HomeViewController
//Notification Observer goes here and call API
let spinner = showLoader(view: self.view) // App goes stuck here and loaded process continuously, response is not getting
Alamofire.request(kURl, method: .post, parameters: param, encoding: URLEncoding.httpBody, headers: nil).authenticate(user: R.string.keys.basicAuthUsername(), password: R.string.keys.basicAuthPassword()).responseSwiftyJSON(completionHandler: {
spinner.dismissLoader()
})
答案 0 :(得分:0)
如@TarasChernyshenko所述,
当您通过didRecieveNotification(_:)
从通知观察者获得回叫时,应用仍然在后台。任何UI更新,例如:
let spinner = showLoader(view: self.view)
必须保留在主线程队列中,如下所示:
DispatchQueue.main.async {
let spinner = showLoader(view: self.view)
//other ui stuffs...
}
答案 1 :(得分:0)
Swift 4.0
根据@TarasChernyshenko声明,我在DispatchQueue.main.async { }
块中放置了发布通知代码,现在它可以正常工作。
DispatchQueue.main.async {
NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "handlePush"), object: dict)
}