Monday, January 16, 2012

Convert Device Token to NSString


Did you had trouble to convert the Device Token you need for sending push messages to NSString?
Have a look at following example:
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
NSString* deviceTokenString = [[NSString alloc]
                initWithString: [[[[devToken description]
                stringByReplacingOccurrencesOfString: @"<" withString: @""]
                stringByReplacingOccurrencesOfString: @">" withString: @""]
                stringByReplacingOccurrencesOfString: @" " withString: @""]];
}


That is the only working solution I was able to find. All other attempts, including converting the value to bytes as suggested by Apple, failed to work.
And don't forget to register your application with

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

somewhere in your Application, preferable within
- (void)applicationDidFinishLaunching:(UIApplication *)app

No comments:

Post a Comment