获取推送通知的设备令牌
我正在推送通知。 我写了下面的代码来获取设备令牌。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Add the view controller's view to the window and display. [self.window addSubview:viewController.view]; [self.window makeKeyAndVisible]; NSLog(@"Registering for push notifications..."); [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; return YES; } - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken]; NSLog(@"This is device token%@", deviceToken); } - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { NSString *str = [NSString stringWithFormat: @"Error: %@", err]; NSLog(@"Error %@",err); }
我能够在设备上成功运行应用程序,但无法在控制台上获取设备ID。
我在authentication和configuration文件方面没有问题。
您必须使用以下代码来获取设备令牌: –
Objective-C的
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; token = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"content---%@", token); }
Swift 3.0
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let tokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) // kDeviceToken=tokenString print("deviceToken: \(tokenString)") }
要获得令牌设备,您可以通过以下几个步骤来完成 :
1)为开发者authentication和分发authentication启用APNS(苹果推送通知服务),然后重新下载这两个文件。
2)重新下载Developer Provisioning和Distribute Provisioning文件。
3)在Xcode界面中:设置PROJECT和TARGETS的configuration有两个文件configuration下载。
4)最后,您需要在AppDelegate文件中添加以下代码以获取令牌设备(注意:在真实设备中运行应用程序)。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self.window addSubview:viewController.view]; [self.window makeKeyAndVisible]; NSLog(@"Registering for push notifications..."); [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; return YES; } - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken]; NSLog(@"%@", str); } - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { NSString *str = [NSString stringWithFormat: @"Error: %@", err]; NSLog(@"%@",str); }
以下代码用于检索设备令牌。
// Prepare the Device Token for Registration (remove spaces and < >) NSString *devToken = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString: @" " withString: @""]; NSString *str = [NSString stringWithFormat:@"Device Token=%@",devToken]; UIAlertView *alertCtr = [[[UIAlertView alloc] initWithTitle:@"Token is " message:devToken delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease]; [alertCtr show]; NSLog(@"device token - %@",str);
如果您还没有获得设备令牌,请尝试使用以下代码注册设备以进行推送通知。
它也将在ios8或更多的工作。
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 if ([UIApplication respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; } #else [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; #endif
Wasif的Swift版本的答案是:
Swift 2.x
var token = deviceToken.description.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<>")) token = token.stringByReplacingOccurrencesOfString(" ", withString: "") print("Token is \(token)")
Swift 3的更新
let deviceTokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
在你的Appdelegate中,在didRegisterForRemoteNotificationsWithDeviceToken
方法中:
NSString *deviceTokenString = deviceToken.description; deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@"[< >]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, deviceTokenString.length)]; NSLog(deviceTokenString.description);
如果您有APN启用证书,则在构build设置中设置代码签署Provision Profile,那么您一定会获得令牌ID。 并删除
configuration文件:自动
并设置为
configuration文件:您的configuration文件证书。
在Swift 3中获取设备令牌
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) print("Device token: \(deviceTokenString)") }
快速获取设备令牌3
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { let token = String(format: "%@", deviceToken as CVarArg).trimmingCharacters(in: CharacterSet(charactersIn: "<>")).replacingOccurrences(of: " ", with: "") print(token) }