ios - 推送通知不一致

标签 ios push-notification apple-push-notifications

工作场景:当我从 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/

相关文章:

ios - 出现键盘时如何移动 View

ios - Cordova 如何在 iOS 上删除 "Push notification"

ios - APNs 证书更改后,FCM 不发送推送通知

iphone - 添加按钮以推送通知警报

ios - 实现基于在 Swift 中作为参数传递的类的动态初始化

ios - Swift + Core ML 从数组中查找相似图像

ios - 发送推送通知而不访问 UDID

c# - 我从 GCM 服务器 {"success":1} But the Notification not Come to the Device 得到了这个响应

node.js - 使用适用于 iOS 应用的 Firebase、Node.js 和 Google Cloud Messaging 推送通知

php - APNs 消息已发送但未在 iOS 设备上接收