2017-01-16 6 views
0

Ich versuche iOS-Gerät Token verwenden Code zu erhalten:iOS ungültig 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]; 
} 

aber ich bekomme Gerät Token "bGb1GbbR17mB/XCWFpH+YpfyprlSvdy2ZN7aqF8QxHE=" im bekommen Token Erfolg Callback-Funktion.

Ich benutze den gleichen Code in einem anderen Projekt kann richtig bekommen device token. warum? Bitte hilf mir!

+0

ja, ich bin immer noch Objective-C – jianchengpan

Antwort

0

Hinweis: Erfordern Push-Benachrichtigung Zertifikat in Ihrem Projekt

hinzufügen Wenn Sie Push-Benachrichtigung Zertifikat hinzufügen als

Try This:

- (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); 
} 

In iOS 10.0 oder länger

#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

vielen Dank für Ihre Antwort verwendet wird, habe ich den Code versuchen, aber ich erhalte gleichen ungültigen Geräte token: ByT3fzJXUkKQ31 + 5B9UOBevFQITgqCX5QJ + CZZS8/KY = – jianchengpan

+0

Sie haben Push-Benachrichtigung Zertifikat? – sohil

+0

ja, in einem anderen Projekt, ich benutze die gleiche Bundle-ID, es funktioniert gut – jianchengpan

1

Versuchen Sie diesen Code:

- (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

danke für Ihren Code, aber ich bekomme immer noch ungültige Gerät Token – jianchengpan

+0

verwenden Sie xcode8? – Niharika

+0

http://Stackoverflow.com/a/40283088/6271729 überprüfen Sie diese .. Könnte nützlich sein. – Niharika

Verwandte Themen