2015-09-07 60 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) 
}