Categories
iPhone Objective C Programming

APNS Send Device Token to Provider

I am currently in the middle of designing and developing an application based on the Apple Push Notification Service. There is alot of information about the client side registration and plenty of server side code examples, but I haven’t found anything that simple demonstrates how to send your device token to the provider service.

The following code is simple (but as a newcomer to Obj-C took me ages to put together). Just press the View Source button on the following panel to see all the code (I am having trouble getting code wrapping to work, so this will be the case until I find a decent syntax highlighter plugin).

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

	NSLog(@"devToken=%@",deviceToken);

	NSString *host = @"yourhost";
	NSString *URLString = @"/register.php?id=";
	URLString = [URLString stringByAppendingString:id];
	URLString = [URLString stringByAppendingString:@"&devicetoken="];

	NSString *dt = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
	dt = [dt stringByReplacingOccurrencesOfString:@" " withString:@""];

	URLString = [URLString stringByAppendingString:dt];
	URLString = [URLString stringByAppendingString:@"&devicename="];
	URLString = [URLString stringByAppendingString:[[UIDevice alloc] name]];

	NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:host path:URLString];
	NSLog(@"FullURL=%@", url);

	NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

	NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}

If you spot the above code and think “why the hell has he done it like that”, it is because I don’t know any better at the moment. If there is a better way please leave some constructive feedback via the comment form.

13 replies on “APNS Send Device Token to Provider”

I am new to using PHP servers, could you please post your register.php file that you are using to receive information from?

@Casey
To be honest there is no register.php file yet. I haven’t written it. But you will need a script to take the query strings that have been sent to it.

Also there is nothing that says you must use PHP as your server side language. If you are more comfortable using .NET or anything for that matter then use that.

My example just shows some code that will send a web request to send the device token.

Some sample PHP code for the above would be

Yes, Apples recommended practice is to run the registration code each and every time the application is ran. I recommend reading the Apple Push Notification Services white paper from developer.apple.com

Token is binary data right ? But with this code, you encode the token as a string. How can it works ?

@Emm
I dont have a complete answer for you but i believe the “description” method of NSData will give you a string dump of the binary data. In this case it is easier to transmit the devicetoken as a string for use in URL’s and then let the provide do what it needs to.

Hey John,

thanks for the code. As you I’m a beginner in iPhoneDev/Objecttive-C too. Maybe you would like to use the “stringWithFormat”-method instead of stringByAppendingString. I think code gets more readable that way.

@Schoerlock
Thanks for the tip. I actually started to program using the same method in PHP using sprintf, it’s a good habbit to get into when dealing with input validation. Using stringByAppendingString was the first way I tried and got the solution working, so that is what I stuck with. My question is why doesn’t ObjC have a simple method of concatenation? Different situations call for different uses of the code.

Cheers,

Jon

Hi John,

thanks for posting your code! It is exactly what i am looking for.
I am still new to Objective-C and therefor my question might be very simple.
In line 7 you adding ‘id’ to URLString. What exactly is behind ‘id’? My compiler doesn’t like it and i can’t figure out what it is.
Can you help me?

thx,
katharina

@katharina
ID is this case is a global variable that I have set that is relevant to my particular application. All you really require to send notifications are the device token, however my application needed more information to be able to link an iPhone to an account.

Hope the helps,

Jon

Thank you for this! It’s what I’ve been looking for, however, when I apply the code to my appDelegate, the token doesn’t seem to be getting passed to the server. returnVariable isn’t being used it looks like?

Hello Guys,

I am new Push notification in iOS. Actually I’m able to see device token in my objective c app now. But, I didn’t get any notification till in my device.What things are remains from my side. Any help will great appreciable.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.