iPhone pushNotification DeviceToken - How to “decr

2019-08-16 15:50发布

问题:

I've already managed to get the devicetoken from APNs. It's type of NSData. So i want to write this deviectoken into my mysql db. I've already tried to convert it to a string without luck. That was my way:

 NSString *tokenTMP = [[NSString alloc] initWithData:devToken encoding:NSASCIIStringEncoding];

If i have the deviceToken in a readable format. How do i use the token in php to send a request to the apns server?

thanks a lot!

回答1:

I added the following category to NSData

- (NSString*) stringWithHexBytes 
{
   NSMutableString *stringBuffer = [NSMutableString stringWithCapacity:([self length] * 2)];
   const unsigned char *dataBuffer = [self bytes];

   for (int i = 0; i < [self length]; ++i)
   {
       [stringBuffer appendFormat:@"%02X", (unsigned long)dataBuffer[ i ]];
   }

   return [[stringBuffer retain] autorelease];
}

Then I can just call [devToken stringWithHexBytes]; and send that up to my server and store it as text.

Hope that helps.

chris.