ios - 单击“已终止”状态下的 Firebase 推送通知时应用程序会卡住

标签 ios swift firebase push-notification

我正在处理推送通知数据,然后在基于推送通知自定义数据的调用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()

    })

最佳答案

swift 4.0

根据 @TarasChernyshenko 声明,我将发布通知代码放入 DispatchQueue.main.async { } block 中,现在工作正常。

DispatchQueue.main.async {

  NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "handlePush"), object: dict)

}

关于ios - 单击“已终止”状态下的 Firebase 推送通知时应用程序会卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48984260/

相关文章:

ios - 如何在动画过程中改变 CALayer 的颜色?

swift - 粒子发射器未显示在图像上(swift spritekit)

ios - 在纵向锁定应用程序中为 MPMoviePlayerViewController 启用横向

arrays - 根据键的值创建多维数组

ios - 更新后在 Firebase 中查找项目的旧值

ios - 加载UIKit View Controller 和.xib作为Cocos2D场景

ios - Sharekit+Cocos2d Facebook 图像共享屏幕在横向模式下位置不正确

ios - 使用 "jiulongw/swift-unity "将 Unity 与 Push Extension 集成到 Swift 项目

java - 我的应用程序由于调用 (activateNotificationsMessages) 函数而崩溃

java - 将变量添加到 android list 文件