【发布时间】:2010-11-23 08:13:39
【问题描述】:
如何从我的 iPhone 设备获取设备令牌?
【问题讨论】:
标签: iphone objective-c ios token apple-push-notifications
如何从我的 iPhone 设备获取设备令牌?
【问题讨论】:
标签: iphone objective-c ios token apple-push-notifications
此方法将在调试模式下在控制台中打印 deviceToken,如果您想查看设备令牌,您也可以在 UIAlert 中看到。
- (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];
}
【讨论】:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 时才会调用它。
如果你实现了这个方法
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
}
对于推送通知,您将获得设备令牌(此方法实际上是您需要在应用程序中实现的两种方法之一)
这可能会很有用http://urbanairship.com/docs/push.html
你也可以看看Push Notification in Iphone application
我希望你觉得这很有用。
【讨论】:
此方法将在控制台中显示您的设备令牌。
- (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);
}
【讨论】: