-1

My app's push notification is working fine in iOS 12.x devices. I am taking the device token as,

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    
    str = [NSString stringWithFormat:@"%@", deviceToken];
    
    
    NSString *token = [[[[deviceToken description]
                         stringByReplacingOccurrencesOfString:@"<"withString:@""]
                        stringByReplacingOccurrencesOfString:@">" withString:@""]
                       stringByReplacingOccurrencesOfString: @" " withString: @""];

from the above method, the token is coming as aJ9tMCx--CQ:APA91bE0UKSbFjPivyvKNHGDxZ5lsqzmiGjBPyYyGMA5Q1mpi5f-zLVreeyk9qbxqOJd4UrEPyeewIic7-HciyXbasd27zx1lpd2_HHCe5QrA5AWEDeC5vGSoE6saiTb6VUTgRT5MaDk

But in iOS 13+ devices, the token is coming as

{length=32,bytes=0x387de70558f0099d606d6cc1234c3967...8a7c5dccb10a67c2}

Since the token isn't in a correct format, the push notification is not working fine. How to get the device token in iOS 13 devices.

Varun P V
  • 1,092
  • 1
  • 12
  • 30
  • Does this answer your question? [How can I convert my device token (NSData) into an NSString?](https://stackoverflow.com/questions/9372815/how-can-i-convert-my-device-token-nsdata-into-an-nsstring) – Desdenova Sep 15 '20 at 15:00
  • The issue is only with the iOS 13 phones. – Varun P V Sep 16 '20 at 17:04

1 Answers1

1

I was not getting the Device token for the iPhone with the os greater than 13. I modified the answer from the below link and finally got the answer.

How can I convert my device token (NSData) into an NSString?

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{

    str2 = [self fetchDeviceToken:deviceToken];

}

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

The Device token will assign to the variable str2.

Varun P V
  • 1,092
  • 1
  • 12
  • 30