Iphone device token - NSData or NSString

2019-01-21 22:18发布

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

6条回答
Animai°情兽
2楼-- · 2019-01-21 22:40

For Swift 3 :

var tokenString: String = ""
    for i in 0..<deviceToken.count {
        tokenString += String(format: "%02.2hhx", deviceToken[i] as CVarArg)
    }

    print(tokenString)

Other Method

Create Data extension for getting hexstring

extension Data {
    var hexString: String {
        return map { String(format: "%02.2hhx", arguments: [$0]) }.joined()
    }
}

And call this extension in

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let tokenString = deviceToken.hexString()
    print("token: \(tokenString)")
}
查看更多
Lonely孤独者°
3楼-- · 2019-01-21 22:42

If anyone is looking for a way to do this in Swift:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""

    for i in 0..<deviceToken.length {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }

    print("tokenString: \(tokenString)")
}

Edit: For Swift 3

Swift 3 introduces the Data type, with value semantics. To convert the deviceToken to a String, you can do as follows:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    var token: String = ""
    for i in 0..<deviceToken.count {
        token += String(format: "%02.2hhx", deviceToken[i] as CVarArg)
    }

    print(token)
}
查看更多
我只想做你的唯一
4楼-- · 2019-01-21 22:43

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.

 - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
 const unsigned *tokenBytes = [deviceToken bytes];
 NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                  ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                  ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                  ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
 [[MyModel sharedModel] setApnsToken:hexToken];

}

We can also store the device token in our NSUserdefaults and use it later to send it out to our server.

查看更多
Animai°情兽
5楼-- · 2019-01-21 22:49

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.

查看更多
放我归山
6楼-- · 2019-01-21 23:01

Ok, I found a solution. If anyone has the same problem, forget about ASCII encoding, just make the string with the following lines:

NSString *deviceToken = [[webDeviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""];
查看更多
不美不萌又怎样
7楼-- · 2019-01-21 23:03

Another way of converting device token into hexa decimal string

NSUInteger capacity = [deviceToken length] * 2;
NSMutableString *stringBuffer = [NSMutableString stringWithCapacity:capacity];
const unsigned char *dataBuffer = [deviceToken bytes];
NSInteger i;
for (i=0; i<[deviceToken length]; ++i) {
    [stringBuffer appendFormat:@"%02X", (NSUInteger)dataBuffer[i]];
}
NSLog(@"token string buffer is %@",stringBuffer);
查看更多
登录 后发表回答