All Products
Search
Document Center

how to get iOS device token's hexadecimal string

Last Updated: Mar 10, 2021

iOS apps will get the deviceToken of the user push service from the APN server. The developer needs to actively bind the deviceToken to the mPaaS push service. During the test development, developers may need to perform message push testing via deviceToken, such as push testing of the Device dimension in the Mobile Push Service (MPS) console. In the MPS console, deviceToken is currently only accepted in hexadecimal string format. One way to obtain this string is as follows:

 
  1. hexstring can be obtained by the following method for earlier version of //iOS 13
  2. NSString *hexTokenString = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
  3. hexTokenString = [hexTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];
  4. if ([hexTokenString length] > 0) {
  5. NSLog(@"push DeviceToken is: %@",hexTokenString);
  6. }
  7. hexstring can be obtained by the following method for //iOS 13
  8. NSUInteger dataLength = deviceToken.length;
  9. if (dataLength == 0) {
  10. return;
  11. }
  12. const unsigned char *dataBuffer = (const unsigned char *)deviceToken.bytes;
  13. NSMutableString *hexTokenString = [NSMutableString stringWithCapacity:(dataLength * 2)];
  14. for (int i = 0; i < dataLength; ++i) {
  15. [hexTokenString appendFormat:@"%02x", dataBuffer[i]];
  16. }
  17. NSLog(@"push DeviceToken token:%@", hexTokenString);