0

I'm trying to send token which I got in didRegisterForRemoteNotificationsWithDeviceToken to the server. But I got an error while sending: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Foundation._SwiftNSData). For requests I use Alamofire framework. My code:

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 shows me: token = 32 bytes. Any suggestions? Maybe I need some additional steps with Data type before send it to the server?

UPDATE

I have tried to convert token to NSString type but got nil

let tokenNSString: NSString? = NSString(data: token, encoding: String.Encoding.utf8.rawValue)
print("nsstrgin from token = \(tokenNSString)")
RomanHouse
  • 2,552
  • 3
  • 23
  • 44
  • 1
    You can covert the NSData into String and send it to server(http://stackoverflow.com/questions/4994302/didregisterforremotenotificationswithdevicetoken-doesnt-invoke-on-calling-regi). If you want to send it as data to server try multipartform upload(http://stackoverflow.com/questions/26121827/uploading-file-with-parameters-using-alamofire). – vishnuvarthan Oct 11 '16 at 08:05
  • @vishnuvarthan see my update, please. – RomanHouse Oct 11 '16 at 08:24
  • Use this to convert into string. http://stackoverflow.com/questions/9372815/how-can-i-convert-my-device-token-nsdata-into-an-nsstring – vishnuvarthan Oct 11 '16 at 09:34

1 Answers1

-1

The deviceToken you are getting inside your project's appDelegate didRegisterForRemoteNotificationsWithDeviceToken is an NSData object. To extract the actual token String from that NSData object use this following code.

  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)

Now, tokenString is your actual token which you are looking for. Try to send this to your server along with any key.

BTW I am using Xcode 7.3.1 with Swift 2.2. Please feel free to modify this as per your requirements.

Thanks, Hope this helped.

onCompletion
  • 6,500
  • 4
  • 28
  • 37
  • You want to send the token string thats why I was trying to explain the procedure. – onCompletion Oct 11 '16 at 09:43
  • You can send your token as nsdata also, y should it only be string ?, is there a disadvantage of sending token as data ? – vishnuvarthan Oct 11 '16 at 09:56
  • Yes you can send as NSData but in that case you server must have the capability to handle that. – onCompletion Oct 11 '16 at 09:57
  • You dont have to worry about that, Now edit and give correct answers to people. – vishnuvarthan Oct 11 '16 at 10:08
  • We are all here to help each other. Don't be rude and before downvoting anyone's answer please dig into it a little that what the answer is going to explain. There are several ways to solve a problem using code. Everyone is having their own point of view and solution.:) Please try to look into some links like this - http://stackoverflow.com/questions/22879445/ios-apns-sending-the-device-token-to-the-provider-in-string-format – onCompletion Oct 11 '16 at 10:19
  • You can share your point of view , but your point of view has to be correct. In this case it is not. Read your answer again , you say sending nsdata is the reason for crash and you saw it is not correct. You are not going to help others by telling incorrect things. – vishnuvarthan Oct 11 '16 at 10:22