【问题标题】:How to Get Device Token in didFinishLaunchingWithOptions如何在 didFinishLaunchingWithOptions 中获取设备令牌
【发布时间】:2013-09-13 11:37:14
【问题描述】:

我在我的第一个 viewcontroller 中调用设备令牌。而且我无法得到结果,因为设备令牌为空。下面是我在appdelegate 中的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge];

    return YES;
}
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    token = [[deviceToken description] stringByTrimmingCharactersInSet:      [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"Device Token ---%@", token);
    [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DeviceToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

当我在 Viewcontroller 中调用时:

NSString *token=  [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceToken"];

令牌为空。

【问题讨论】:

  • 你在模拟器上试试吗?
  • 用真机试试。
  • 检查您的配置文件并在真实设备中构建。

标签: iphone ios objective-c token devicetoken


【解决方案1】:
    NSString *device = [deviceToken description];
    device = [device stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    device = [device stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSLog(@"My device is: %@", device);

这在我的设备中非常适合我。

【讨论】:

    【解决方案2】:

    请参阅此 SO 答案中的 Kulss 答案:

    How can I convert my device token (NSData) into an NSString?

    您应该解析字节而不是描述。

    【讨论】:

    • 这绝对是正确答案。其他答案是建议的解决方案可能无法证明未来。
    【解决方案3】:

    第一

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

    将此添加到Application.h

    【讨论】:

      【解决方案4】:

      我的朋友试试这个....

          - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
                    #if !TARGET_IPHONE_SIMULATOR
      
      // Prepare the Device Token for Registration (remove spaces and < >)
      token = [[[[devToken description]
                              stringByReplacingOccurrencesOfString:@"<"withString:@""]
                             stringByReplacingOccurrencesOfString:@">" withString:@""]
                            stringByReplacingOccurrencesOfString: @" " withString: @""];
      NSLog(@"%@",token);
      [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"deviceToken"];
      [[NSUserDefaults standardUserDefaults] synchronize];
              #endif
      }
      /**
        * Failed to Register for Remote Notifications
       */
        - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
          #if !TARGET_IPHONE_SIMULATOR
      
         NSLog(@"Error in registration. Error: %@", error);
        #endif
        }
      

      编码愉快!!!!

      【讨论】:

      • 谢谢,但我正在使用真实设备进行检查。请检查我上面的评论。
      • 这仅适用于我的朋友...检查我的行#if !TARGET_IPHONE_SIMULATOR
      【解决方案5】:

      添加两个方法didRegister和didFailToRegister,并确认你是在didRegister还是在didFailedToRegister中收到调用。

      -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
      {
          NSLog(@"Device Token=> %@",deviceToken);
      
          //Parse your device toke
      
      }
      
      -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
      {
          NSLog(@"Error in registration. Error: %@", error.description);
      }
      

      并确保您是否成功获得了您的设备令牌。或者你注册远程失败...

      【讨论】:

      • 我也添加了这个方法。我成功了。但问题是我的 viewcontroller 在加载之前加载了 device token 。为此,设备令牌为空?现在清楚了吗?
      • 注册远程通知后为什么不加载你的视图控制器。
      【解决方案6】:

      你可以试试这个

      - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*) deviceToken {
          NSString *pushToken = [deviceToken description];
          pushToken = [pushToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
          [[NSUserDefaults standardUserDefaults] setObject:pushToken forKey:@"device_token_data"];
          [[NSUserDefaults standardUserDefaults] synchronize];
      }
      

      【讨论】:

        【解决方案7】:

        可能对你有帮助。

        首先注册通知然后您将收到didRegisterRemoteNotification

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        {
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
        }
        
        - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {   
            self.strdeviceToken=[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
            self.strdeviceToken = [self.strdeviceToken stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            self.strdeviceToken=[self.strdeviceToken stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
        }
        

        【讨论】:

          【解决方案8】:

          您应该检查您的配置文件。此外,应用程序应配置为在开发人员门户上推送通知。 执行这些步骤。

          1. 在开发者门户上为您的应用程序标识符启用推送通知。
          2. 重新生成配置文件。
          3. 下载并安装新的配置文件。
          4. 构建并运行。 它会起作用的。

          【讨论】:

            【解决方案9】:

            试试这个..

            事务完成后,您需要在视图控制器中使用令牌编号。因此,尝试通过AppDelegate.m 中的类似方法在 appDelegate 中再次创建或更改根视图控制器。

            -(void)changeRootView
            {
                [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
                 (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
            
                // Override point for customization after application launch.
                self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
                //Adding navigation controller to the main view.
                [navigationController release];
            
                navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
                self.window.rootViewController = navigationController;
                [self.window makeKeyAndVisible];
            
            }
            

            在这个方法中

            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
                     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
            

            此语句有助于再次调用didRegisterForRemoteNotificationsWithDeviceToken,您可以将令牌存储在诸如NSUserDefaults 之类的对象中,您可以在viewController 中使用它。这个对我有用。你可以试试这个。祝你好运。

            【讨论】:

              猜你喜欢
              • 2012-12-15
              • 2012-10-02
              • 2015-12-02
              • 2014-05-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-01-02
              相关资源
              最近更新 更多