I am receiving iPhone device token in the form of NSData
object.
When I tested my notifications script function, I have only copied that object from log and the notifications went fine. However when I try now to automatically do it, I am sending the device token as ASCII encoded string in the form of variable
self.deviceToken = [[NSString alloc] initWithData:webDeviceToken encoding:NSASCIIStringEncoding];
The string that I am getting has some funky characters and looks similar to this "å-0¾fZÿ÷ʺÎUQüRáqEªfÔk«"
When server side script sends the notification to that token, I am not receiving anything.
Do I need to decode something and how?
Regardz
For Swift 3 :
Other Method
Create Data extension for getting hexstring
And call this extension in
If anyone is looking for a way to do this in Swift:
Edit: For Swift 3
Swift 3 introduces the
Data
type, with value semantics. To convert thedeviceToken
to a String, you can do as follows:I found this solution better as iOS can change the usage of description in future versions, so using description property on data can be unreliable in future. We can directly use this by creating hex Token from the data token bytes.
}
We can also store the device token in our NSUserdefaults and use it later to send it out to our server.
I don't think it's a good solution, as you have to reconstruct the string before sending the notifications to Apple servers. Use Base64 encoding for transmitting the strings or something similar.
Ok, I found a solution. If anyone has the same problem, forget about ASCII encoding, just make the string with the following lines:
Another way of converting device token into hexa decimal string