iOS APNS: sending the device token to the provider in string format
Solution 1
You are right that you have to convert the device token from NSData
to NSString
to
be able to send it with a JSON object. But what conversion method you choose is completely
up to you or the requirements of the provider. The most common methods are a
hex string (see for example Best way to serialize an NSData into a hexadeximal string) or a Base64 string (using
base64EncodedStringWithOptions
). Both are 100% "reliable".
Also you should always send the device token to the provider and not only when it has changed. The provider has to keep a database of all device tokens with a timestamp of when it was sent last recently, in order to compare the timestamp with a possible response from the "feedback service".
Solution 2
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 doing above 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 above listed steps you can use this delegate function to retrieve and handle push notification once it comes.The below added method will fire either the app is running in background or not.The method given below is available from ios7.0
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
Solution 3
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 count it. Removing spaces and <> is really not a good idea
AppsDev
Updated on June 04, 2022Comments
-
AppsDev about 2 years
I need to send the APNS device token of my iOS app to my provider by calling a service that expects JSON data in my request. I'm reading Apple's Local and Push Notification Programming Guide and it only says that the
application:didRegisterForRemoteNotificationsWithDeviceToken:
delegate method passes the device token asNSData
and you should pass it to your provider encoded in binary data. But I need it to be converted to string in order to be able to send a JSON request to my provider.I've also been reading several posts related to this, since it looks it is a common scenario, but I've found some different ways to convert such device token to string to send it, and I'm not sure which of them should be the most appropriate. Which would the most reliable way to deal with this be? I suppose my provider will need to convert this string back to call APNS, and I also need to store this token in the app in order to safely compare it with the new value if a new token is generated and
application:didRegisterForRemoteNotificationsWithDeviceToken:
is called, to send the token only if it has changed.Thanks
-
AppsDev about 10 yearsThanks, I finally used the code you indicated to create a hexadecimal string
-
Carlos Ricardo almost 9 yearsMakes a lot more sense than the other future error prone "description" approaches! Thanks!
-
Rahul K Rajan over 8 yearsThis is my understanding to implement pushnotification,if you find any mistakes with this code please feel free to addd.....
-
David over 7 yearsHad similar problem, the solution was to follow your answer which was to remove all "space" characters from the device token. This was for PushSharp not sure if this is a general guideline for the Push Notification API.