2016-10-11 1 views
0

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 유형에 토큰을 변환하려고하지만이 deviceToken 프로젝트의 AppDelegate에 didRegisterForRemoteNotificationsWithDeviceToken 내부에 점점 것은 NSData 객체이다 nil

let tokenNSString: NSString? = NSString(data: token, encoding: String.Encoding.utf8.rawValue) 
print("nsstrgin from token = \(tokenNSString)") 
+1

NSData를 String으로 변환하고 서버 (http://stackoverflow.com/questions/4994302/didregisterforremotenotificationswithdevicetoken-doesnt-invoke-on-calling-regi)로 보낼 수 있습니다. 서버로 데이터를 보내려면 multipartform upload (http://stackoverflow.com/questions/26121827/uploading-file-with-parameters-using-alamofire)를 시도하십시오. – vishnuvarthan

+0

@vishnuvarthan 내 업데이트를 참조하십시오. – RomanHouse

+0

문자열로 변환하려면이 항목을 사용하십시오. http://stackoverflow.com/questions/9372815/how-can-i-convert-my-device-token-nsdata-into-an-nsstring – vishnuvarthan

답변

-1

을 가지고있다. 해당 NSData 객체에서 실제 토큰 문자열을 추출하려면 다음 코드를 사용하십시오.

let tokenChars = UnsafePointer<CChar>(deviceToken.bytes) 
    var tokenString: String = "" 
    for i in 0..<deviceToken.length { 
    tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]]) 
    } 
    print("This is My Device Token for Push notification -", tokenString) 

자, 찾고있는 실제 토큰은 tokenString입니다. 이것을 임의의 키와 함께 서버에 보내십시오.

현재 내가 Swift 2.2에서 Xcode 7.3.1을 사용하고 있습니다. 귀하의 요구 사항에 따라 수정하십시오.

감사합니다. 도움이 되셨습니다.

+0

왜 NSData 개체가 올바르지 않습니까? – vishnuvarthan

+0

당신은 내가 왜 절차를 설명하려했는지 토큰 문자열을 보내고 싶습니다. – Tuhin

+0

당신은 nsdata로 당신의 토큰을 보낼 수 있습니다, y는 단지 문자열이어야합니까?, 데이터로 토큰을 보내는 단점이 있습니까? – vishnuvarthan