【问题标题】:How can I get the device token for the iOS 13+ devices in Objective c如何在 Objective c 中获取 iOS 13+ 设备的设备令牌
【发布时间】:2021-01-02 07:55:09
【问题描述】:

我的应用的推送通知在 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 设备中获取设备令牌。

【问题讨论】:

标签: objective-c apple-push-notifications


【解决方案1】:

我没有获得操作系统大于 13 的 iPhone 的设备令牌。我从以下链接修改了答案,终于得到了答案。

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

【讨论】:

    猜你喜欢
    • 2020-01-21
    • 2015-12-02
    • 2020-02-04
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多