Get device token for push notification Get device token for push notification ios ios

Get device token for push notification


NOTE: The below solution no longer works on iOS 13+ devices - it will return garbage data.

Please use following code instead:

+ (NSString *)hexadecimalStringFromData:(NSData *)data{  NSUInteger dataLength = data.length;  if (dataLength == 0) {    return nil;  }  const unsigned char *dataBuffer = (const unsigned char *)data.bytes;  NSMutableString *hexString  = [NSMutableString stringWithCapacity:(dataLength * 2)];  for (int i = 0; i < dataLength; ++i) {    [hexString appendFormat:@"%02x", dataBuffer[i]];  }  return [hexString copy];}

Solution that worked prior to iOS 13:

Objective-C

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];    NSLog(@"this will return '32 bytes' in iOS 13+ rather than the token", token);} 

Swift 3.0

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){    let tokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})    print("this will return '32 bytes' in iOS 13+ rather than the token \(tokenString)")}


To get Token Device you can do by some steps:

1) Enable APNS (Apple Push Notification Service) for both Developer Certification and Distribute Certification, then redownload those two file.

2) Redownload both Developer Provisioning and Distribute Provisioning file.

3) In Xcode interface: setting provisioning for PROJECT and TARGETS with two file provisioning have download.

4) Finally, you need to add the code below in AppDelegate file to get Token Device (note: run app in real device).

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {         [self.window addSubview:viewController.view];     [self.window makeKeyAndVisible];     NSLog(@"Registering for push notifications...");         [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];     return YES;}- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {      NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken];     NSLog(@"%@", str);}- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {      NSString *str = [NSString stringWithFormat: @"Error: %@", err];     NSLog(@"%@",str);}


Using description as many of these answers suggest is the wrong approach - even if you get it to work, it will break in iOS 13+.

Instead you should ensure you use the actual binary data, not simply a description of it. Andrey Gagan addressed the Objective C solution quite well, but fortunately it's much simpler in swift:

Swift 4.2 works in iOS 13+

// credit to NSHipster (see link above)// format specifier produces a zero-padded, 2-digit hexadecimal representationlet deviceTokenString = deviceToken.map { String(format: "%02x", $0) }.joined()