后台请求不执行Alamofire Swift

我试图在POST,GET等后台进行调用,以便在didReceiveRemoteNotification方法中更精确,因为它们开始作为推送通知到达.我的问题是,在我打开应用程序之前,所有Alamofire.request都不会在后台模式中调用.我现在有

我试图打开一个会话,但它不会使请求工作.

这些是我想在后台执行的(后台手机)

Alamofire.Manager(configuration: configuration).request(.GET,url,parameters: nil)
                        .responseJSON { (_,_,JSON,_) in
                            //println(JSON)
                                println(JSON)
                REST OF THE CODE

但它不会起作用,即使我在这些请求下面添加代码也可以工作,但是请求的返回甚至是请求都没有.

虽然方法说“后台配置”,但它实际上意味着网络会话配置为 to allow for interruptions and continuation上传/下载.您需要做的是延长应用程序的执行时间,使其即使在后台运行一段时间

有beginBackgroundTaskWithExpirationHandler:这是专门为此而设计的.当您使用它时,您将获得更多的时间来执行您需要的任何内容(在此限制之后,无论如何,您的应用程序将被终止,现在您的应用程序立即终止).

您可以编写以下方法

func beginBackgroundTask() -> uibackgroundtaskIdentifier {
    return UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({})
}

func endBackgroundTask(taskID: uibackgroundtaskIdentifier) {
    UIApplication.sharedApplication().endBackgroundTask(taskID)
}

当您想要使用它时,您只需在开始/完成下载调用时简单地开始/结束任务:

// Start task
let task = self.beginBackgroundTask()

// Do whatever you need,like download of the images
self.someBackgroundTask()

...

// End task once everything you need to do is done
self.endBackgroundTask(task)

希望能帮助到你!

编辑1:

如果问题是您的下载方法永远不会被调用,那么这意味着您没有在通知有效负载中发送正确的数据:

For a push notification to trigger a download operation,the notification’s payload must include the content-available key with its value set to 1. When that key is present,the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app. 07001

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...