Device ID NULL

I have a weird problem that I cannot reproduce on my own. Some of my users are returning empty (or zero) device tokens for Apple Push Notification. This probably happens on 5% of users. Someone had the same problem or is aware of this.

My code for getting device token:

- (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


source to share


2 answers


You should not manipulate the device token in this way, and especially not use a method description

that is a debugging helper rather than a string conversion operator.

From UIApplicationDelegate

link
:



deviceToken

A token that identifies the device for APS. The token is an opaque data type because it is the form that the provider must submit to the APS servers when a notification is sent to the device. APS servers require a binary format for performance reasons.

The device token size is 32 bytes.

Note that the device token is different from the unique identifier property of the UIDevice because for security and privacy reasons it must be changed when the device is wiped.

Store the device token in binary.

+3


source


I did this:

- (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);

      



Disadvantage. You cannot read a marker that is always 32 bytes long. This may change one day. So you don't know the size of the token, passing it somewhere via NULL with a terminated char array. For example, a token can contain a NULL character. So it prefers to convert the token's binary data using base64 or hex or something like that, or pass it in binary + size form.

0


source







All Articles