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()
}
它不工作了。我添加額外的處理程序?
你很對。我們不需要後臺會話配置,默認配置就足夠了,因爲當我們回覆操作應用狀態時已經轉向運行。謝謝 :) – zippo