如何获取设备令牌?

时间:2012-10-11 19:25:13

标签: iphone objective-c ios xcode

我将用户名,密码和devicetoken的值发布到.net webservice。但它没有获得任何设备令牌值。我正在使用下面的代码。

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

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{    
   NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken); 
   NSString *dt = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
   dt = [dt stringByReplacingOccurrencesOfString:@" " withString:@""]; 
   self.DeviceToken=dt; 
   NSLog(@"~~~~devToken(dv)=%@",deviceToken);
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{      
    NSLog(@"Failed to get token, error: %@", error);
}

但在控制台显示

  

无法获取令牌,错误:错误Domain = NSCocoaErrorDomain Code = 3000“找不到应用程序的有效'aps-environment'权利字符串”UserInfo = 0x1a0810 {NSLocalizedDescription =找不到应用程序的有效'aps-environment'权利字符串< / p>

有什么想法吗?提前谢谢!

4 个答案:

答案 0 :(得分:2)

您是否在临时文件中启用了推送通知?

通过the tutorial检查您是否做得对

首先尝试从您的mac向设备发送推送通知,如教程中所述。您需要在服务器端使用pem文件,您需要从iOS Portal创建:)

答案 1 :(得分:1)

我假设您正在设备上运行/调试,因为您尝试从模拟器注册推送令牌时会遇到其他错误。只是想把它弄清楚。

话虽如此,通常会导致您看到的错误的原因是您选择的配置文件中未启用推送通知。现在,您可能已经进入iOS Provisioning Portal并为您的App ID启用了Push(如果您已为您的开发或分发配置文件启用了Push,请注意)。但是,在执行此操作之后,您必须进入“脏”供应门户的供应配置文件,以生成其中包含推送权利的新供应配置文件。

通过'弄脏'它我的意思是进入并更改配置文件的某些设置以强制重新创建。如果当你回到配置文件列表时,你会知道你是否“弄脏”它,状态会暂时变为'待定'几秒钟,然后再次变为'活动'并允许你下载它。

哦,我刚刚发现了这个也回答了这个问题(如果我把它链接错了,请有人帮帮忙):Bundle Identifier and push certificate... aps-environment entitlement error

答案 2 :(得分:0)

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{    
   NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken); 
   NSString *dt = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
   dt = [dt stringByReplacingOccurrencesOfString:@" " withString:@""]; 
   self.DeviceToken=dt; 

}

答案 3 :(得分:0)

要获取设备令牌,在设备中运行应用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Let the device know we want to receive push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
        (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    return YES;
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken);
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
}