工作场景:当我将我的应用程序从 xcode 直接运行到我的设备时,我可以在服务器上运行推送通知,它按预期工作。
非工作场景:我将应用程序导出到 IPA 并通过 iTunes 在我的设备上安装应用程序。当我从服务器推送通知时,我会收到以下错误ERROR: Unable to send message ID 1: Invalid token (8).
在写这篇文章时,我有一个想法并检查了来自 xcode 安装和 IPA 安装的设备 ID,它们是不同的!
将设备 ID 发送到我的服务器的代码:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// You can send here, for example, an asynchronous HTTP request to your web-server to store this deviceToken remotely.
// convert token to a single string
NSString *token = [[[deviceToken description]
stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]
stringByReplacingOccurrencesOfString:@" "
withString:@""];
NSString *post = [NSString stringWithFormat:[NSString stringWithFormat:@"token=%@", token]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://mywebsitename.com/ApnsPHP/add.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
NSError *e = nil;
NSDictionary *dict = [XMLReader dictionaryForXMLData:data error:&e];
NSLog(@"response from server: %@", dict);
});
}];
NSLog(@"Did register for remote notifications: %@", deviceToken);
}
如何获取它以便 IPA 分发中的设备令牌通过?谢谢。