0

For getting push notification here i am sending postitem, token, like count and currentname using alamofire post method(pod version alamofire 4.5). I did not get any response when post method called and it does not show any errors. I tried keeping breaking points in alamofire function, it call alamofire.requestion then it goes out function.

Here is the code tried to send post method to backend:

  func postNotification(postItem: String, post: Post) {

    print("Get token from post:::",post.token)
    print(postItem)
    let token = UserDefaults.standard.string(forKey: "token")




    let headers: HTTPHeaders = ["Content-Type" :"application/x-www-form-urlencoded"]

       let parameters : [String:Any] = ["count":post.likeCount!, "likedby":currentName, "postId=":postItem, "token": post.token!]
    Alamofire.request("http://highavenue.co:9000/likesnotification/", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):
            if let data = response.result.value{
                print(data)
            }
            break

        case .failure(_):
            print(response.result.error as Any)
            break

        }
    }

}

Getting console error like this

 2018-07-10 14:21:07.980212+0530 HighAvenue[10584:4236493] Task <B5FC98AB-C3FE- 
4D4F-9A93-72D3FFE35DF7>.<1> finished with error - code: -1001
Optional(Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." 
UserInfo={NSUnderlyingError=0x1c0e478f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://highavenue.co:9000/likesnotification/, NSErrorFailingURLKey=http://highavenue.co:9000/likesnotification/, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.})
PvDev
  • 791
  • 21
  • 67
  • have you checked it with other tools like postman... what was it response – Santhosh Jul 10 '18 at 10:59
  • @santhosh { "status": "success", "code": 200, "error": "" } – PvDev Jul 10 '18 at 11:10
  • Ok then i think the problem is with encoding try once with removing the encoding from the parameters or by using urlencoding.default. So, that it takes the default value – Santhosh Jul 10 '18 at 11:14
  • @santhosh how to do that.. – PvDev Jul 10 '18 at 11:15
  • replace Alamofire.request("http://highavenue.co:9000/likesnotification/", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil) with Alamofire.request("http://highavenue.co:9000/likesnotification/", method: .post, parameters: parameters) – Santhosh Jul 10 '18 at 12:11
  • @santhosh it does not work..finished with error - code: -1002 Optional(Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSUnderlyingError=0x1c5048fd0 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=highavenue.co:9000/likesnotification/, NSErrorFailingURLKey=highavenue.co:9000/likesnotification/, NSLocalizedDescription=unsupported URL}) – PvDev Jul 10 '18 at 12:26
  • @santhosh tried by giving http://highavenue.co:9000/likesnotification/ too – PvDev Jul 10 '18 at 12:28
  • test with adding http:// as well i think it should work – Santhosh Jul 10 '18 at 12:49
  • yeah tested http:// stille it does not work.. – PvDev Jul 10 '18 at 12:54
  • @santhosh let myParams = "count=\(post.likeCount!)&likedby=\(currentName)&postId=\(postItem)&token=\(post.token!)" let headers: HTTPHeaders = ["Content-Type" :"application/x-www-form-urlencoded"] let parameters : [String:Any] = ["count":post.likeCount!, "likedby":currentName, "postId=":postItem, "token": post.token!] Alamofire.request("http://highavenue.co:9000/likesnotification/", method: .post, parameters: parameters).responseJSON { (response:DataResponse) in ------------this correct or not??] – PvDev Jul 10 '18 at 12:56
  • @santhosh https://drive.google.com/file/d/1dGYwSl8_jRz_cM6TUNXQTJWT3IMaQqDf/view?usp=sharing can you check out this link – PvDev Jul 10 '18 at 13:07
  • I think in parameters "postId=":postItem should be "postId":postItem – Santhosh Jul 11 '18 at 06:30

1 Answers1

0

That is because you are not setting request time in your network call, by default your request time is a small interval, so please increase request timeout time. something like this,

let request = NSMutableURLRequest(url: URL(string: "")!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.timeoutInterval = 120 // 120 secs
let values = ["key": "value"]
request.httpBody = try! JSONSerialization.data(withJSONObject: values, options: [])
Alamofire.request(request as! URLRequestConvertible).responseJSON {
    response in
    // do whatever you want here
}

Second mistake in your code is you are trying to access http url which are by default are not allowed so you have to by pass this security from your app, Please refer to this answer in order to remove this security layer from your app. The resource could not be loaded because the App Transport Security policy requires the use of a secure connection

Najam
  • 1,129
  • 11
  • 32
  • Thanks for answering. it gets crashed . Could not cast value of type 'NSMutableURLRequest' (0x1036f8d10) to 'Alamofire.URLRequestConvertible' (0x105a851b8). 2018-07-10 14:52:43.253861+0530 HighAvenue[10598:4246892] Could not cast value of type 'NSMutableURLRequest' (0x1036f8d10) to 'Alamofire.URLRequestConvertible' (0x105a851b8). – PvDev Jul 10 '18 at 09:23
  • 1
    you are probably trying to access Almofire.request that is something different, please make a request object just like this and then pass it to Almofire's function – Najam Jul 10 '18 at 09:26
  • Sorry! i could not get you. – PvDev Jul 10 '18 at 09:43
  • 1
    no, problem, just compare your code with the chunk I provided you. you are passing arguments with in one line and I am creating a request object and then passing it to function. All I am saying is clean up your code and make this object of request and set time interval of 120 seconds. – Najam Jul 10 '18 at 09:45
  • you can check this answer for time interval ...https://stackoverflow.com/questions/36626075/handle-timeout-with-alamofire?rq=1 – Najam Jul 10 '18 at 09:45