ios 如何获取设备令牌?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12847029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get device token?
提问by fathik
I am posting values of username,password and devicetoken to .net webservice. But It didn't get any device token value. I am using below code.
我将用户名、密码和设备令牌的值发布到 .net 网络服务。但它没有得到任何设备令牌值。我正在使用下面的代码。
- (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);
}
But in console it shows
但在控制台中它显示
Failed to get token, error: Error Domain=NSCocoaErrorDomain Code=3000 "no valid 'aps-environment' entitlement string found for application" UserInfo=0x1a0810 {NSLocalizedDescription=no valid 'aps-environment' entitlement string found for application
获取令牌失败,错误:错误域=NSCocoaErrorDomain Code=3000“没有为应用程序找到有效的‘aps-environment’授权字符串” UserInfo=0x1a0810 {NSLocalizedDescription=没有为应用程序找到有效的‘aps-environment’授权字符串
Any idea? Thanks in advance!
任何的想法?提前致谢!
采纳答案by DivineDesert
Have you enabled push notification in your provisional file ?
您是否在临时文件中启用了推送通知?
Go through the tutorialto check if you have done right
通过教程检查你是否做对了
First try sending push notification to device from your mac, as mentioned in tutorial. You will need a pem file on server side that you need to create from iOS Portal :)
首先尝试从您的 Mac 向设备发送推送通知,如教程中所述。您需要在服务器端创建一个从 iOS 门户创建的 pem 文件:)
回答by Jai Govindani
I'm assuming you're running/debugging on a device since you'd get a different error trying to register for a push token from the simulator. Just wanted to get that out of the way.
我假设您正在设备上运行/调试,因为尝试从模拟器注册推送令牌时会遇到不同的错误。只是想摆脱它。
That being said, what usually causes the error you're seeing is that Push Notifications aren't enabled in the Provisioning Profile that you've selected. Now, you might have gone in to the iOS Provisioning Portal and enabled Push for your App ID (also note if you've enabled Push for your Development or Distribution profile). However, after doing that, you have to go in and 'dirty' your Provisioning Profile for the Provisioning Portal to generate a new Provisioning Profile that has the push entitlements in it.
话虽如此,通常导致您看到的错误的原因是您选择的配置文件中未启用推送通知。现在,您可能已经进入 iOS Provisioning Portal 并为您的 App ID 启用了推送(还要注意您是否为您的开发或分发配置文件启用了推送)。但是,在执行此操作之后,您必须进入并“弄脏”供应门户的供应配置文件,以生成包含推送权利的新供应配置文件。
By 'dirtying' it I mean going in and changing some setting of the profile to force a re-creation. You'll know if you 'dirtied' it enough if when you go back to the list of Provisioning Profiles, the status changes temporarily to 'Pending' for a few seconds before becoming 'Active' again and allowing you to download it.
“弄脏”是指进入并更改配置文件的某些设置以强制重新创建。如果当您返回配置文件列表时,状态会暂时更改为“待处理”几秒钟,然后再次变为“活动”并允许您下载它,您就会知道您是否“弄脏”了它。
Oh, and I just found this which also answers the question (someone help out if I linked it wrong please): Bundle Identifier and push certificate... aps-environment entitlement error
哦,我刚刚发现这也回答了这个问题(如果我链接错了,请有人帮忙):Bundle Identifier and push certificate... aps-environment entitlement error
回答by Bhushan_pawar
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken);
NSString *dt = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
dt = [dt stringByReplacingOccurrencesOfString:@" " withString:@""];
self.DeviceToken=dt;
}
回答by Vineesh TP
To get device token, Run application in device.
要获取设备令牌,请在设备中运行应用程序。
- (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);
}