【问题标题】:how to get Device Token in iOS?如何在 iOS 中获取设备令牌?
【发布时间】:2015-12-02 13:14:50
【问题描述】:

我正在处理推送通知。我编写了以下代码来获取设备令牌。

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

【问题讨论】:

标签: ios objective-c swift devicetoken


【解决方案1】:

在 iOS 8 和 iOS 9 中,您需要像这样注册通知:

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

请注意,如果您还想支持 iOS 7,则需要在早期版本的 iOS 上调用现有代码。

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题,所以你必须使用以下代码来获取设备令牌:-

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

    即使那样它也不起作用然后请检查您的配置文件,它应该是您创建推送通知的 ssl 证书的应用 ID。

    【讨论】:

      【解决方案3】:

      试试这个代码:

       // 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 仅在具有有效 APNS 证书的设备中返回

      【讨论】:

      • 你的笔记我认为被低估了,我会强调它,因为它解决了我的一个大问题
      【解决方案4】:

      在 Xcode 中启用“Push Notifications”,这将解决问题。

      Targets -> Capabilities -> Push Notifications
      

      注意: Provisioning Profiles应该处于Active状态

      【讨论】:

        【解决方案5】:

        Swift 3

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

        【讨论】:

          【解决方案6】:

          这里是swift 4.0的最新代码,所以你可以使用下面的代码来获取设备令牌。

          import UserNotifications
          
          if #available(iOS 10, *) {
                      UNUserNotificationCenter.current().delegate = self
                      UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in
                      }
                      UIApplication.shared.registerForRemoteNotifications()
                  } else {
                      UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
                      UIApplication.shared.registerForRemoteNotifications()
                  }
          
          
          func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
                  let token = deviceToken.map { String(format: "%.2hhx", $0) }.joined()
          
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-08-17
            • 1970-01-01
            • 1970-01-01
            • 2021-01-02
            • 1970-01-01
            • 1970-01-01
            • 2017-04-13
            相关资源
            最近更新 更多