Iphone 如何在didFinishLaunchingWithOptions中获取设备令牌

Iphone 如何在didFinishLaunchingWithOptions中获取设备令牌,iphone,ios,objective-c,token,devicetoken,Iphone,Ios,Objective C,Token,Devicetoken,我正在我的第一个视图控制器中调用设备令牌。我无法得到结果,因为设备令牌为空。下面是我在appdelegate中的代码: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRem

我正在我的第一个
视图控制器中调用设备令牌。我无法得到结果,因为设备令牌为空。下面是我在
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];
}

令牌为空。

试试这个,我的朋友

    - (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
  }
-(void)应用程序:(UIApplication*)应用程序DIDregisterForRemotionTificationswithDeviceToken:(NSData*)devToken{
#如果!目标\u IPHONE\u模拟器
//准备要注册的设备令牌(删除空格和<>)
令牌=[[[devToken description]
StringByReplacingOfString:@“with String:@”]
StringByReplacingOfString:@“和字符串:@]”发生;
NSLog(@“%@”,令牌);
[[NSUserDefaults standardUserDefaults]setObject:token forKey:@“deviceToken”];
[[NSUserDefaults standardUserDefaults]同步];
#恩迪夫
}
/**
*无法注册远程通知
*/
-(无效)应用程序:(UIApplication*)应用程序未能注册远程通知,错误为:(N错误*)错误{
#如果!目标\u IPHONE\u模拟器
NSLog(@“注册错误。错误:%@”,错误);
#恩迪夫
}

快乐编码!!!!

试试这个,我的朋友

    - (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
  }
-(void)应用程序:(UIApplication*)应用程序DIDregisterForRemotionTificationswithDeviceToken:(NSData*)devToken{
#如果!目标\u IPHONE\u模拟器
//准备要注册的设备令牌(删除空格和<>)
令牌=[[[devToken description]
StringByReplacingOfString:@“with String:@”]
StringByReplacingOfString:@“和字符串:@]”发生;
NSLog(@“%@”,令牌);
[[NSUserDefaults standardUserDefaults]setObject:token forKey:@“deviceToken”];
[[NSUserDefaults standardUserDefaults]同步];
#恩迪夫
}
/**
*无法注册远程通知
*/
-(无效)应用程序:(UIApplication*)应用程序未能注册远程通知,错误为:(N错误*)错误{
#如果!目标\u IPHONE\u模拟器
NSLog(@“注册错误。错误:%@”,错误);
#恩迪夫
}

快乐编码!!!!

添加两种方法didRegister和didFailedToRegister,并确认您在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);
}

并确保您成功获取设备令牌。或者您未能注册或删除…

添加两种方法didRegister和didFailtToRegister,并确认您在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];
}
-(void)应用程序:(UIApplication*)应用程序DIDregisterforRemotionTificationswithDeviceToken:(NSData*)deviceToken{
NSString*pushToken=[deviceToken描述];
pushToken=[pushToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@”“];
[[NSUserDefaults standardUserDefaults]setObject:pushToken-forKey:@“设备\令牌\数据”];
[[NSUserDefaults standardUserDefaults]同步];
}
你可以试试这个

- (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];
}
-(void)应用程序:(UIApplication*)应用程序DIDregisterforRemotionTificationswithDeviceToken:(NSData*)deviceToken{
NSString*pushToken=[deviceToken描述];
pushToken=[pushToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@”“];
[[NSUserDefaults standardUserDefaults]setObject:pushToken-forKey:@“设备\令牌\数据”];
[[NSUserDefaults standardUserDefaults]同步];
}
可能会对您有所帮助

首先在中注册通知,然后您将在
didRegisterRemotonification

- (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];
}
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
{
[[UIApplication sharedApplication]RegisterForRemotionTificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
-(无效)应用程序:(UIApplication*)应用程序DIdRegisterForRemotionTificationswithDeviceToken:(NSData*)deviceToken{
self.strdeviceToken=[[deviceToken description]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@”“];
self.strdeviceToken=[self.strdeviceToken stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
self.strdeviceToken=[self.strdeviceToken stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
}
可能会对您有所帮助

首先在中注册通知,然后您将在
didRegisterRemotonification

- (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];
}
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
{
[[UIApplication sharedApplication]RegisterForRemotionTificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
-(无效)应用程序:(UIApplication*)应用程序DIdRegisterForRemotionTificationswithDeviceToken:(NSData*)deviceToken{
self.strdeviceToken=[[deviceToken description]stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@”“];
self.strdeviceToken=[self.strdeviceToken stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
self.strdeviceToken=[self.strdeviceToken stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
}

您应该检查您的资源调配配置文件。此外,应用程序还应该配置为在开发者门户上发送推送通知。 为此,请执行以下步骤

  • 在开发人员门户上为应用程序标识符启用推送通知
  • 重新生成配置文件
  • 下载并安装新的资源调配配置文件
  • 构建并运行。 它会起作用的

  • 你应该检查你的资源调配配置文件。此外,应用程序还应该配置为在开发者门户上发送推送通知。 为doin执行以下步骤
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions