2015-09-07 141 views
4

我正在推送通知。我编写了下面的代码来获取设备令牌。如何在iOS中获取设备令牌?

-(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(@"This is device token%@", deviceToken); 
    } 

-(void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err 
{ 
     NSString *str = [NSString stringWithFormat: @"Error: %@", err]; 
     NSLog(@"Error %@",err);  
} 
+5

可能重复的[获取推送通知的设备令牌](http://stackoverflow.com/questions/8798725/get-device-token-for-push-notification) – Yuyutsu

回答

10

试试这个代码:

// Register for Push Notification 


if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 
    { 
     [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
     [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    } 
    else 
    { 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; 
    } 

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings // NS_AVAILABLE_IOS(8_0); 
{ 
     [application registerForRemoteNotifications]; 
    } 

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

    NSLog(@"deviceToken: %@", deviceToken); 
    NSString * token = [NSString stringWithFormat:@"%@", deviceToken]; 
    //Format token as you need: 
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    token = [token stringByReplacingOccurrencesOfString:@">" withString:@""]; 
    token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""]; 

} 

注意:模拟器无法返回deviceToken,deviceToken仅在设备在Xcode返回与有效的APNS证书

2

在iOS系统中8和iOS 9,你需要为这样的注册通知:

NSLog(@"Registering for push notifications..."); 
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert categories:nil]]; 
[[UIApplication sharedApplication] registerForRemoteNotifications]; 

注意,如果你也想支持的iOS 7,那么你就需要调用现有的代码在早期版本的iOS上。我

1

同样的问题发生,所以你必须使用下面的代码获取设备令牌: -

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    NSLog(@"content---%@", token); 
} 

即使这样它不工作,那么请检查您的provisioning profile,它应该是应用程序ID通过它你已经创建了推送通知的SSL证书。

2

启用 “推送通知”,这将解决问题。

Targets -> Capabilities -> Push Notifications 

Attached image for reference

注: 预置描述文件应在活动国家

-1

获取device token斯威夫特3

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    let token = String(format: "%@", deviceToken as CVarArg) 
     .trimmingCharacters(in: CharacterSet(charactersIn: "<>")) 
     .replacingOccurrences(of: " ", with: "") 
    print(token) 
}