0

如标题所述,当远程通知到来时,我可以回复为文本。如果我点击一次主页按钮,我的 http 请求运行良好,但在应用程序未运行时不起作用,它会等待。当应用程序启动时,它也可以工作。

// Please work
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
    print(userInfo["personB"])
    if identifier == "comment-reply" {
        if let response = responseInfo[UIUserNotificationActionResponseTypedTextKey],
            responseText = response as? String {
            let request = NSMutableURLRequest(URL: NSURL(string: "https://example.com/post-ios.php")!)
            request.HTTPMethod = "POST"
            let p = "a=123&b=\(responseText)"
            request.HTTPBody = p.dataUsingEncoding(NSUTF8StringEncoding)
            let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
            task.resume()
        }
    }
    completionHandler()
}

现在,我需要:-VoIP 证书、-background 会话配置、-dispatch 或-upload 任务吗?

谁能帮我?

更新上传任务

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
    let id = userInfo["personB"]!
    if identifier == "comment-reply" {
        if let response = responseInfo[UIUserNotificationActionResponseTypedTextKey],
            responseText = response as? String {
            let conf = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("replyPost")
            let requ = NSMutableURLRequest(URL: NSURL(string: "https://example.com/post-ios.php")!)
                requ.HTTPMethod = "POST"
            let data = "a=123&b=\(responseText)".dataUsingEncoding(NSUTF8StringEncoding)
            let task = NSURLSession(configuration: conf, delegate: self, delegateQueue: NSOperationQueue.mainQueue()).uploadTaskWithRequest(requ, fromData: data!)
            task.resume()
        }
    }
    completionHandler()
}

它也不起作用。我要添加额外的处理程序吗?

4

1 回答 1

1

当您调用 completionHandler() 时,您的应用程序将再次暂停。这将在您的任务完成之前发生。

而不是uploadTask(with:from:)你应该调用uploadTask(with:from:completionHandler:)并放入completionHandler()完成处理程序块。

于 2016-09-20T23:25:37.810 回答