2014-09-05 148 views
3

我有一个奇怪的问题,我无法自行复制。我的一些用户正在为Apple推送通知返回空白(或空)设备令牌。它可能发生在5%的用户身上。 任何人都有同样的问题或知道这件事。设备令牌NULL

我为获得设备令牌代码是:

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

    NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; 
    [def setObject:tokenString forKey:@"deviceToken"]; 

} 

- (void)application: (UIApplication*)application didFailToRegisterForRemoteNotificationsWithError: (NSError*)error 
{ 
    NSLog(@"Failed to get token, error: %@", error) ; 
} 

回答

3

你不应该操纵设备令牌以这种方式,特别是不使用description方法,这是一种调试辅助,而不是一个型转换以字符串运算符。

UIApplicationDelegate reference

deviceToken

标识了设备到APS的标记。令牌是 不透明数据类型,因为这是提供程序在向设备发送通知时需要提交给APS服务器的形式。 出于性能原因,APS服务器需要二进制格式。

设备令牌的大小是32个字节。

请注意,设备令牌与UIDevice的uniqueIdentifier 属性不同,因为出于安全和隐私的原因,它必须在设备被擦除时更改。

以二进制形式存储设备令牌。

+1

顺便说一句,我得到了答案。那些用户正在使用越狱手机:) – 2014-09-05 07:19:30

+1

@ArsalanHaider我的答案依然存在;您没有希望使用您的代码重新创建设备令牌。 – trojanfoe 2014-09-05 07:24:01

0

我做了这样的说法:

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { 
    NSUInteger devicetokenlen=[deviceToken length]; 
    char* devicetoken=(char*)malloc(devicetokenlen+1); 
    memcpy(devicetoken,[deviceToken bytes],devicetokenlen); 
    devicetoken[devicetokenlen]=0; 
    //... 
    free(devicetoken); 

的缺陷是你不能假设令牌始终为32个字节长。 它可能有一天会改变。所以当你通过NULL终止的字符数组传递给某个地方时,你不知道它的大小。例如,令牌可能包含NULL字符。所以它更倾向于使用base64或hex或类似的东西来转换令牌二进制数据,或者以二进制数据+大小的形式传递它。