0

如标题中所述,当远程通知到达时,我可以以文本的形式回复。如果我点击主页按钮一次,我的http请求就可以正常工作,但在应用程序没有运行时它不会运行,它会等待。当应用程序启动时,它也可以工作。ios - NSURLSession with dataTaskWithRequest仅在应用程序启动时触发,而不在应用程序未运行时触发

// 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任务?

任何人都可以帮助我吗?

UPDATE上传任务

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() 
} 

它不工作了。我添加额外的处理程序?

回答

1

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

而不是uploadTask(with:from:)您应该拨打uploadTask(with:from:completionHandler:)并将completionHandler()放在完成处理程序块中。

+0

你很对。我们不需要后台会话配置,默认配置就足够了,因为当我们回复操作应用状态时已经转向运行。谢谢 :) – zippo

相关问题