How to get Device Token from my iPhone Device?
Asked
Active
Viewed 1.9k times
3 Answers
14
this method will print the deviceToken in console in debug mode, if you want to see the device token you can see in UIAlert also.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"APN device token: %@", deviceToken);
NSString *deviceTokenString = [NSString stringWithFormat:@"%@",deviceToken];
UIAlertView *deviceTokenAlert = [[UIAlertView alloc] initWithTitle:@"Device Token"
message:deviceTokenString
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
}

User97693321
- 3,336
- 7
- 45
- 69
-
2Thanks for your answer ! Do you know if this device token can change over time, for an iPhone, or is it constant ? – darksider Aug 11 '12 at 23:03
-
1I have always observerd that device token is constant, but I think it may change also. – User97693321 Aug 12 '12 at 01:29
-
1check the answer of this question http://stackoverflow.com/questions/6927011/is-the-device-token-as-unique-as-the-device-id, it says only when you restore backups will the device token change. – CarmeloS Dec 18 '12 at 10:23
-
Note that this will only get called if you call `[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];` first. – Aaron Brager Feb 20 '13 at 21:33
7
If you have implemented this method
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
}
for Push Notification then you will get the device token (This method is actually one of the two methods that you require to implement in the application)
This might find it useful http://urbanairship.com/docs/push.html
you can also look at Push Notification in Iphone application
I hope you find this useful.

Community
- 1
- 1

Rahul Sharma
- 3,013
- 1
- 20
- 47
6
This method will show your device token in console.
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *str = [NSString
stringWithFormat:@"%@",deviceToken];
NSString *newString = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
newString = [newString stringByReplacingOccurrencesOfString:@"<" withString:@""];
newString = [newString stringByReplacingOccurrencesOfString:@">" withString:@""];
[[NSUserDefaults standardUserDefaults] setObject:newString forKey:@"deviceToken"];
NSLog(@"Your deviceToken ---> %@",newString);
}

Akshay
- 2,973
- 6
- 43
- 75