我试图将我在didRegisterForRemoteNotificationsWithDeviceToken
中获得的令牌发送到服务器。但发送时出现错误:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Foundation._SwiftNSData)
。对于请求我使用Alamofire
框架。我的代码:将推送通知的令牌发送到服务器。 Alamofire错误
func signUp(withToken token: Data, completion: (Error) -> Void) {
let parameters: Parameters = ["registration_id": token]
print("token = \(token)")
Alamofire.request(baseUrl + signUpPath, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON(completionHandler: {response in
})
}
Print
显示我:token = 32 bytes
。有什么建议么?在发送给服务器之前,可能需要使用Data
类型的其他步骤?
UPDATE
我试图令牌转换为NSString
型,但得到nil
let tokenNSString: NSString? = NSString(data: token, encoding: String.Encoding.utf8.rawValue)
print("nsstrgin from token = \(tokenNSString)")
你可以将NSData转换为字符串并发送给服务器(http://stackoverflow.com/questions/4994302/didregisterforremotenotificationswithdevicetoken-doesnt-invoke-on-calling-regi)。如果你想把它作为数据发送到服务器,请尝试multipartform上传(http://stackoverflow.com/questions/26121827/uploading-file-with-parameters-using-alamofire)。 – vishnuvarthan
@vishnuvarthan请参阅我的更新。 – RomanHouse
用此转换成字符串。 http://stackoverflow.com/questions/9372815/how-can-i-convert-my-device-token-nsdata-into-an-nsstring – vishnuvarthan