IOS APNS: sending device token to vendor in string format

I need to send the APNS device token of my iOS application to my provider by calling a service that expects JSON data in my request. I read the Apple Local and Push Notification Programming Guide , and it only says that the delegate method application:didRegisterForRemoteNotificationsWithDeviceToken: passes the device token as NSData and you must pass it to the binary encoded provider. But I need it to be converted to a string in order to send a JSON request to my provider.

I also read a few posts related to this, as it looks like this is a normal scenario, but I found several ways to convert the device token to a string to send it, and I'm not sure which of them should be the most suitable. What will be the most reliable way to handle this? I believe that my provider will need to convert this string back to call APNS, and I also need to save this token in the application in order to safely compare it with the new value if a new token is generated and application:didRegisterForRemoteNotificationsWithDeviceToken: to send the token, only if he has changed.

thanks

+8
ios string-formatting apple-push-notifications devicetoken
source share
4 answers

You are correct that you need to convert the device token from NSData to NSString to be able to send it using a JSON object. But which conversion method you choose is completely up to you or the requirements of the supplier. The most common methods are hex string (see, for example, Best way to serialize NSData in a hexadecimal string ) or Base64 string (using base64EncodedStringWithOptions ). Both are 100% reliable.

Also, you should always send the device token to the supplier, and not just when it has been changed. The provider must keep a database of all device tokens with a timestamp when it was last sent recently in order to compare the timestamp with a possible response from the “feedback service”.

+10
source share

In didFinishLaunchingWithOptions method

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; } 

After executing on the lines of code, add the method below

 #pragma mark Push Notifications - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *token_string = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString: @" " withString: @""]; NSString* strURL = [NSString stringWithFormat:@"http://www.sample.com?device_token=%@&type=IOS",token_string]; strURL=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%@",strURL); NSData *fileData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]]; NSLog(@"content---%@", fileData); } 

After the steps above, you can use this delegate function to retrieve and process push notifications after it appears. The method below starts either the application is running in the background or not. The method below is available from ios7.0

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler 
+8
source share
 const unsigned *tokenBytes = [deviceToken bytes]; NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x", ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]), ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]), ntohl(tokenBytes[6]), ntohl(tokenBytes[7])]; 

Converting data to bytes means we can read it. Removing spaces and <> is really not a good idea.

+5
source share
 - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)_deviceToken { NSString *str = [NSString stringWithFormat:@"%@",_deviceToken]; //replace '<' and '>' along with spaces before you send it to the server. } 

it works reliably for me on almost all web platforms.

0
source share

All Articles