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

相關問題