Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iphone device token - NSData or NSString

I am receiving iPhone device token in the form of NSData object. When I tested my notifications script function, I have only copied that object from log and the notifications went fine. However when I try now to automatically do it, I am sending the device token as ASCII encoded string in the form of variable

self.deviceToken = [[NSString alloc] initWithData:webDeviceToken encoding:NSASCIIStringEncoding]; 

The string that I am getting has some funky characters and looks similar to this "å-0¾fZÿ÷ʺÎUQüRáqEªfÔk«"

When server side script sends the notification to that token, I am not receiving anything.

Do I need to decode something and how?

Regardz

like image 595
Mladen Avatar asked Oct 19 '09 07:10

Mladen


People also ask

How can I convert my device token NSData into an NSString?

It's my solution and It works well in my app: NSString* newToken = [[[NSString stringWithFormat:@"%@",deviceToken] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""]; convert NSData to NSString with stringWithFormat.

What is a device token for Iphone?

A device token is an identifier for the Apple Push Notification System for iOS devices. Apple assigns a Device Token on a per-app basis (iOS 7 and later) which is used as a unique identifier for sending push notifications.

Do device tokens change iOS?

With the release of iOS 7, device tokens no longer are static and never changing once assigned, but now can change without warning; causing device tokens to be an unreliable method for addressing iOS devices.

What is device token in Apple Push Notification?

Register your app with APNs and receive a globally unique device token, which is effectively the address of your app on the current device. Your provider server must have this token before it can deliver notifications to the device.


1 Answers

Ok, I found a solution. If anyone has the same problem, forget about ASCII encoding, just make the string with the following lines:

NSString *deviceToken = [[webDeviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; deviceToken = [deviceToken stringByReplacingOccurrencesOfString:@" " withString:@""]; 
like image 101
Mladen Avatar answered Sep 20 '22 15:09

Mladen