相同的代码第二天工作
该活动application:didRegisterForRemoteNotificationsWithDeviceToken:deviceToken现在(第二天)开始。请注意,这似乎与询问您是否要启用推送通知的警报无关,因为当它开始工作时我没有看到此警报。
我仍然觉得奇怪的一件事是,昨天使用 Ad Hoc 构建,application:didRegisterForRemoteNotificationsWithDeviceToken:deviceToken每次我启动应用程序时都会触发该事件。但是,使用开发人员配置文件的 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 的描述将返回一个以“<”开头并以“>”结尾的十六进制字符串。中间是空格。我最初的方法是使用将 NSData 转换为字符串,NSUTF8StringEncoding但这是错误的(因为您希望将令牌作为十六进制字符串)。