在 Ad Hoc 构建中,我看到调用了 application:didRegisterForRemoteNotificationsWithDeviceToken: 事件,但当我使用开发人员配置文件使用 DEBUG 构建时没有调用。 Ad Hoc 和 Developer Provisioning Profile 使用相同的 App ID。
应用 ID 截图:
当使用开发人员配置文件运行时,我确实看到了询问是否启用推送通知的警报。我选择使用推送通知,但事件 application:didRegisterForRemoteNotificationsWithDeviceToken: 从未发生。请注意,application:didFailToRegisterForRemoteNotificationsWithError: 的事件也不会发生。
请注意,在 application:didFinishLaunchingWithOptions: 中,我有:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert];
// Other code...
}
我对这个很困惑。
其他注意事项:
最佳答案
事件 application:didRegisterForRemoteNotificationsWithDeviceToken:deviceToken 正在运行(第二天)。请注意,这似乎与询问您是否要启用推送通知的警报无关,因为它开始工作时我没有看到此警报。
我仍然觉得奇怪的一件事是,昨天使用 Ad Hoc 构建事件 application:didRegisterForRemoteNotificationsWithDeviceToken:deviceToken 每次我启动应用程序时都会触发。然而,具有开发人员配置文件的 DEBUG 构建根本不会触发。今天早上使用相同的代码(并且每次使用带有开发人员配置文件的 DEBUG 构建时都会触发)......设备中的某些东西(内部计时器/超时)必须已重置。
下面是我在接收 deviceToken 事件中使用的代码。它使用 Urban Airship 作为 Apple 推送通知服务器 (APNS) 的提供者。我已经从代码中删除了用户名和密码。我希望这对正在尝试解决问题的其他人有用。
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *tokenStr = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"Device Token (raw): %@", deviceToken);
NSLog(@"Device Token (string): %@", tokenStr);
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Device Token"
message:tokenStr
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil] autorelease];
[alert show];
// Code to send token to server
NSString *urlString = [NSString stringWithFormat:
@"https://go.urbanairship.com/api/device_tokens/%@", tokenStr];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:
[NSURL URLWithString:urlString]];
#if APP_STORE_RELEASE
[request addBasicAuthenticationHeaderWithUsername:@"aaaaaaaaaaaaaaaaaaaaaa"
andPassword:@"bbbbbbbbbbbbbbbbbbbbbb"];
#else
[request addBasicAuthenticationHeaderWithUsername:@"cccccccccccccccccccccc"
andPassword:@"dddddddddddddddddddddd"];
#endif
request.requestMethod = @"PUT";
[request startAsynchronous];
}
请注意,NSData 的描述将返回一个以“<”开头并以“>”结尾的十六进制字符串。中间是空格。我最初的方法是使用 NSUTF8StringEncoding 将 NSData 转换为字符串,但这是错误的(因为您希望将 token 作为十六进制字符串)。
”开头并以“>
关于ios - 应用程序 :didRegisterForRemoteNotificationsWithDeviceToken: Not Firing When Using Developer Provisioning Profile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9320668/