2017-01-16 50 views
0

我試圖讓iOS設備使用的令牌代碼:的iOS無效deviceToken

if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
    UNUserNotificationCenter *center = [UNUserNotificationCenter   currentNotificationCenter]; 
    center.delegate = self; 
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge |  UNAuthorizationOptionSound | UNAuthorizationOptionAlert |  UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) { 
     if (!error) { 
      NSLog(@"request authorization succeeded!"); 
     } 
    }]; 

    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    #else 
    UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge); 
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; 
    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
#endif 
} else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { 
    UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge); 
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; 
    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
} else { 
    UIRemoteNotificationType apn_type = (UIRemoteNotificationType) (UIRemoteNotificationTypeAlert | 
                    UIRemoteNotificationTypeSound | 
                    UIRemoteNotificationTypeBadge); 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type]; 
} 

,但我得到設備標識"bGb1GbbR17mB/XCWFpH+YpfyprlSvdy2ZN7aqF8QxHE="獲得令牌成功回調函數

我在另一個項目中使用相同的代碼可以得到正確的device token。 爲什麼?請幫幫我!

+0

是的,我還在使用Objective-C – jianchengpan

回答

0

注:需要添加推送通知證書的項目

如果你有添加推送通知證書比

試試這個:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) 
    { 
     [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 

     [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    } 
    else 
    { 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
     (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; 
    } 

} 

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

    self.AppDeviceToken=[[NSString alloc] initWithFormat:@"%@",deviceToken]; 
    //NSLog(@"My token is: %@", self.AppDeviceToken); 

    self.AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    self.AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@"<" withString:@""]; 
    self.AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@">" withString:@""]; 

    NSLog(@"%@'s Device Token is : %@",[[UIDevice currentDevice] name],self.AppDeviceToken); 
} 

在iOS系統10.0或更高

#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")) 
    { 
     UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
     center.delegate = self; 
     [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){ 
      if(!error){ 
       [[UIApplication sharedApplication] registerForRemoteNotifications]; 
      } 
     }]; 
    } 
} 

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings 
{ 
    //register to receive notifications 
    [application registerForRemoteNotifications]; 
} 

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler 
{ 
    NSLog(@"User Info : %@",notification.request.content.userInfo); 
    completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge); 
} 

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler 
{ 
    NSLog(@"User Info : %@",response.notification.request.content.userInfo); 
    completionHandler(); 
} 

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken 
{ 
    self.AppDeviceToken=[[NSString alloc] initWithFormat:@"%@",deviceToken]; 
    //NSLog(@"My token is: %@", self.AppDeviceToken); 

    self.AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    self.AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@"<" withString:@""]; 
    self.AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@">" withString:@""]; 

    NSLog(@"%@'s Device Token is : %@",[[UIDevice currentDevice] name],self.AppDeviceToken); 
} 
+0

感謝您的回答,我有嘗試的代碼,但我得到同樣無效的設備令牌:ByT3fzJXUkKQ31 + 5B9UOBevFQITgqCX5QJ + CZZS8/KY = – jianchengpan

+0

您有使用推送通知證書嗎? – sohil

+0

是的,在另一個項目中,我使用相同的捆綁ID,它工作正常 – jianchengpan

1

試試這個代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    if(CheckOSVersion >= 8.0) 
    { 
     [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
     [[UIApplication sharedApplication] registerForRemoteNotifications]; 

     UIUserNotificationSettings* currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; 
     UIUserNotificationType enabledTypes = currentSettings.types; 

     BOOL turnedOffFromWithinNotificaitonCenter = ((enabledTypes & UIUserNotificationTypeAlert) == UIUserNotificationTypeAlert); 

     if (turnedOffFromWithinNotificaitonCenter){ 
     } 
     else{ 
     } 
    } 
    else 
    { 
     UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; 

     if (types & UIRemoteNotificationTypeAlert) 
     { 
     } 
     else 
     { 
     } 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 
    } 
} 

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

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    NSString *devToken = [[[[deviceToken description] 
          stringByReplacingOccurrencesOfString:@"<"withString:@""] 
          stringByReplacingOccurrencesOfString:@">" withString:@""] 
          stringByReplacingOccurrencesOfString: @" " withString: @""]; 
    [AppDelegate instance].strToken = devToken; 
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Push"]; 
    [[NSUserDefaults standardUserDefaults] setValue:devToken forKey:@"deviceToken"]; 
    [[NSUserDefaults standardUserDefaults]synchronize]; 
    NSLog(@"Device Token of Device %@",devToken); 
} 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
    NSLog(@"Userinfo%@",userInfo); 
} 
+0

感謝您的代碼,但我仍然得到無效的設備令牌 – jianchengpan

+0

您使用的是xcode8嗎? – Niharika

+0

http://stackoverflow.com/a/40283088/6271729看看這個..可能會有用。 – Niharika