2017-04-05 260 views
1

我试图在使用Alamofire的swift中使用firebase API。我在添加标题时遇到问题,并且我的请求说在我的调用中有一个额外的参数。使用Swift 3和alamofire的Firebase API 4

public static func broadcastSyncDo(localId : String){ 
     let headers: HTTPHeaders = [ 
      "Authorization": "key=xxx", 
      "Content-Type": "application/json" 
     ] 

     let parameters : [String : String] = [ 
      "to" : "/topics/"+localId, 
      "content_available" : "1", 
      "priority" : "high" 
     ] 


     Alamofire.request(.post, util_Constants.FIREBASE_API, headers : headers, parameters: parameters, encoding: URLEncoding.default) 
      .responseJSON { response in 
       print("response : \(response)") 
     } 
    } 
+0

这是一个实时服务更好地使用它自己的方法 –

+0

@ÖzgürErsil你是什么意思?我需要从我的设备发送推送通知。如果它是从GCM控制台发送的,它们将是相同的 – user2363025

回答

1

似乎有一个小虫子与雨燕3.0和Alamofire 4.You必须确保所有的变量是完全正确的类型,否则你会得到错误“呼叫额外的参数”。以下是你的代码应该看起来像所有正确的类型。

//requestString variable must be of type [String] 
let requestString : String = "https://yourURLHere" 

//headers variable must be of type [String: String] 
let headers: [String: String] = [ 
      "Authorization": "key=xxx", 
      "Content-Type": "application/json" 
     ] 

//parameters variable must be of type [String : Any] 
let parameters : [String : Any] = [ 
      "to" : "/topics/"+localId, 
      "content_available" : "1", 
      "priority" : "high" 
     ] 
//Alamofire 4 uses a different .request syntax that Alamofire 3, so make sure you have the correct version and format. 
Alamofire.request(requestString, method: .post, parameters: parameters, encoding: 
      URLEncoding.default, headers).responseString(completionHandler: 
    { responds in 
    //some response from JSON code here. 
}) 

此链接(Alamofire Swift 3.0 Extra parameter in call)也将有助于进一步解释为什么你得到这个错误。

相关问题