工作场景:当我从 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 分发的设备 token 通过?谢谢。
最佳答案
Xcode install 是您的开发版本,.ipa 是生产/临时版本,它们有不同的证书。仔细阅读远程通知指南!
关于ios - 推送通知不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11370671/