如何获取 Objective c 中 iOS 13+ 台设备的设备令牌

How can I get the device token for the iOS 13+ devices in Objective c

我的应用程序的推送通知在 iOS 12.x 台设备上运行良好。我将设备令牌作为

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

根据上述方法,令牌将以 aJ9tMCx--CQ:APA91bE0UKSbFjPivyvKNHGDxZ5lsqzmiGjBPyYyGMA5Q1mpi5f-zLVreeyk9qbxqOJd4UrEPyeewIic7-HciyXbasd27zx1lpd2_HHCe5QrA5AWEDeC5vGSoE6saiTb6VUTgRT5MaDk

的形式出现

但在 iOS 13+ 设备中,令牌将作为

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

由于令牌格式不正确,推送通知无法正常工作。如何在 iOS 13 个设备中获取设备令牌。

我没有得到 iPhone 的设备令牌 os 大于 13。我修改了下面 link 的答案,最后得到了答案。

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];
}

设备令牌将分配给变量str2