7

I have an iPhone app I'm working on and trying to get Amazon SNS set up to test PNS. When we register the app with APNS, it gives a 32-digit device token (873DBDDA-17CF-4A24-88C6-990B90AFC4C3). When registering a device with Amazon SNS, it says the device token must be 64-digits long. What am I missing here?

Brad Herman
  • 9,665
  • 7
  • 28
  • 30

3 Answers3

11

How did you get that token? It doesn't look like a correct APNS device token. A real one will be 64 hex digits. Here's the code I use:

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *tokenstring = [[[deviceToken description]
                              stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]
                             stringByReplacingOccurrencesOfString:@" " withString:@""];

    // pass tokenstring to your APNS server
}

The token that I get out of that method looks like this:

 8ec3bba7de23cda5e8a2726c081be79204faede67529e617b625c984d61cf5c1
Rakesh patanga
  • 832
  • 9
  • 25
jsd
  • 7,673
  • 5
  • 27
  • 47
  • I agree, I get pns tokens with 64 digits, not 32 – Rob Bowman May 12 '14 at 17:08
  • 1
    It is 64 characters in hex string form but the [documentation](https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW4) could confuse you as it refers to the binary form `Device token - 32 bytes- The device token in binary form, as was registered by the device.` – KCD Oct 19 '15 at 23:11
0

This is an old question, but I was looking for a problem with token's and this unanswered question came up. Here is what I use - AWS v2.

With help from erik-aigner in the question 7520615

- (void)awsStartWithDeviceToken:(NSData *)deviceToken {

    // Get a hex string for the NSData deviceToken
    // https://stackoverflow.com/questions/7520615/how-to-convert-an-nsdata-into-an-nsstring-hex-string
    NSUInteger dataLength = [deviceToken length];
    NSMutableString *deviceTokenString = [NSMutableString stringWithCapacity:dataLength*2];
    const unsigned char *dataBytes = [deviceToken bytes];
    for (NSInteger idx = 0; idx < dataLength; ++idx) {
        [deviceTokenString appendFormat:@"%02x", dataBytes[idx]];
    }
    _savedDeviceTokenFormatted = deviceTokenString;
}
Community
  • 1
  • 1
Kent
  • 1,374
  • 1
  • 15
  • 29
0

A Swift extension to convert to hexadecimal string

extension Data {

    /// Return hexadecimal string representation of Data bytes
    public var hexadecimalString: String {
        var bytes = [UInt8](repeating: 0, count: count)
        copyBytes(to: &bytes, count: count)

        let hexString = NSMutableString()
        for byte in bytes {
            hexString.appendFormat("%02x", UInt(byte))
        }

        return String(hexString)
    }
}
Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76