繁体   English   中英

如何在didFinishLaunchingWithOptions中获取设备令牌

[英]How to Get Device Token in didFinishLaunchingWithOptions

我在第一个viewcontroller调用设备令牌。 而且我无法获得结果,因为设备令牌为null。 以下是我在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"];

令牌为空。

    NSString *device = [deviceToken description];
    device = [device stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    device = [device stringByReplacingOccurrencesOfString:@" " withString:@""];

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

这对我的设备非常适合。

请参阅此SO解答中的Kulss回答:

如何将设备令牌(NSData)转换为NSString?

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

第一

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

Application.h添加它

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

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

编码愉快!!!

添加两个方法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);
}

并确保您成功获得了您的设备令牌。 或您未能注册远程...

你可以试试这个

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

可能会帮助您。

首先注册通知,然后您将在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];
}

您应该检查您的配置文件。 另外,应将应用程序配置为在开发人员门户上进行推送通知。 请执行以下步骤。

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

试试这个

交易完成后,您需要视图控制器中的令牌编号。 因此,为此尝试通过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中使用它。 这个对我有用。 你可以试试看 好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM