2017-04-05 201 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)也將有助於進一步解釋爲什麼你得到這個錯誤。

相關問題