0

タイトルで説明したように、リモート通知が来るとテキストとして返信できます。一度ホームボタンをタップするとhttpリクエストがうまく動作しますが、アプリが実行されていないときは機能しません。アプリが起動されると、それも機能します。 -VoIP証明書、 -backgroundセッション構成、 -dispatch何か、 または -uploadタスクを: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() 
} 

は今、私は必要なのですか?

誰でも手伝ってもらえますか?アップロードタスク

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

ため

UPDATEは、それはあまりにも動作しますしません。ハンドラを追加しますか?

答えて

1

completionHandler()を呼び出すと、アプリケーションは再び中断されます。これは、タスクが完了する前に発生します。

uploadTask(with:from:)の代わりにuploadTask(with:from:completionHandler:)を呼び出し、completionHandler()を補完ハンドラブロックに入れる必要があります。

+0

あなたはかなり正しいです。バックグラウンドセッションの設定は必要ありません。デフォルトの設定で十分です。アクションアプリの状態が返ってきたら、既に実行中です。ありがとう:) – zippo

関連する問題