iOS:生产推送通知,来自 APNS 服务器的无效令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22568648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: Production push notifications, Invalid token from APNS server
提问by James Harpe
My app is now available in the app store, so I've downloaded it to my device. The push notifications were working fine during development. I am using JavaPNS to send out the notifications, and I have switch it to point to Apple's production servers.
我的应用程序现在可以在应用程序商店中使用,因此我已将其下载到我的设备上。推送通知在开发过程中运行良好。我正在使用 JavaPNS 发送通知,并且我已将其切换为指向 Apple 的生产服务器。
However, I'm now getting an Invalid Token error back from the APNS servers.
但是,我现在从 APNS 服务器收到无效令牌错误。
I have the Archive scheme set to "Release", and I have Release set to use this distribution profile:
我将存档方案设置为“发布”,并将发布设置为使用此分发配置文件:
Inside that provisioning file, you can see that I have the environment set correctly:
在该配置文件中,您可以看到我正确设置了环境:
Yet I still get the error. When I look in my database, I think the device token the app is returning to me is the same as the development one, so that could be the problem. But I don't know why it would be returning that, given that the app is signed correctly. This is a device I also used for testing, could that be a problem? Any other ideas about what's happening here?
但是我仍然收到错误。当我查看我的数据库时,我认为应用程序返回给我的设备令牌与开发的相同,所以这可能是问题所在。但我不知道为什么它会返回,因为该应用程序已正确签名。这是我也用来测试的设备,这会不会有问题?关于这里发生的事情的任何其他想法?
Thanks!
谢谢!
EDIT: I'm not storing a token in my code, Eran's answer suggests that the only other possibilities are an old token in my database, or the app not being signed by a production profile. I'm cleared my database, so I know it's not the former, and as for the latter, I don't see how that could be the case, since I only have one distribution profile, and as I've shown above, it has the "aps-environment" key set correctly. XCode wouldn't even let me use a development profile for app store submission, would it? A few other possibilities:
编辑:我没有在我的代码中存储令牌,Eran 的回答表明唯一的其他可能性是我的数据库中的旧令牌,或者应用程序没有被生产配置文件签名。我清除了我的数据库,所以我知道它不是前者,至于后者,我不明白这是怎么回事,因为我只有一个分布配置文件,正如我上面所示,它正确设置了“aps-environment”键。XCode 甚至不会让我使用应用商店提交的开发配置文件,是吗?其他几种可能性:
Is it possible that something being wrong with key I'm sending with my notifications could cause "Invalid Token"? If so, can I regenerate this key for my existing profiles?
我随通知发送的密钥有问题可能会导致“无效令牌”吗?如果是这样,我可以为我现有的配置文件重新生成此密钥吗?
Isn't there another provisioning profile contained in the AppID for the purposes of push notifications? Could a problem with that cause the invalid token error?
AppID 中是否包含另一个用于推送通知的配置文件?是否有问题会导致无效令牌错误?
回答by James Harpe
I re-downloaded the push production certificate and exported it from the keychain as .p12. This seems to have solved the problem. It seems strange though that a bad private key was giving me the "Invalid Token" error.
我重新下载了推送生产证书并将其从钥匙串中导出为 .p12。这似乎解决了问题。尽管错误的私钥给了我“无效令牌”错误,但这似乎很奇怪。
回答by Eran
The device token for production is different than the development one, so if you send a development token to production APNS servers (or vica versa), that's the cause of the problem.
用于生产的设备令牌与开发令牌不同,因此如果您将开发令牌发送到生产 APNS 服务器(反之亦然),这就是问题的原因。
It's possible you didn't remove the development device token from your server when you switched to production.
当您切换到生产时,您可能没有从服务器中删除开发设备令牌。
Or if you are getting the development token from your production app, then either the app is returning a locally stored device token (which you may have stored on your device during development) instead of asking Apple to get a new one, or (if you do ask Apple for a current token and still get the development token) your app is not signed with the production provisioning profile.
或者,如果您从生产应用程序中获取开发令牌,则该应用程序返回本地存储的设备令牌(您可能在开发期间已将其存储在设备上),而不是要求 Apple 获取新令牌,或者(如果您请向 Apple 索要当前令牌并仍然获得开发令牌)您的应用程序未使用生产配置文件进行签名。
回答by Lewis Edward Garrett
MyApp is an iOS app that sends and receives Push Notifications MyApp-to-MyApp using APN token IDs fetched from a database that is kept updated by each MyApp at logon. The Push Notifications are sent by MyApp using the NWPusherframework API.
MyApp 是一个 iOS 应用程序,它使用从每个 MyApp 在登录时保持更新的数据库中获取的 APN 令牌 ID 来发送和接收推送通知 MyApp 到 MyApp。推送通知由 MyApp 使用NWPusher框架 API 发送。
As long as the project was run in Developer (Debug) mode, sent and received Push Notifications worked perfectly. But, in Production mode, sending Push Notifications would not work.
只要项目在开发者(调试)模式下运行,发送和接收推送通知就可以完美运行。但是,在生产模式下,发送推送通知将不起作用。
I went back and completely recreated my AppID, SSL certificates, provisions, etc., but to no avail. Next I used the NWPusherMacOS Pusherapp to experiment with sending Push Notifications to MyApp. I configured Pusherto send a Push Notification (to the tokenID of an Ad Hocdistribution of MyApp) with MyApp's Production SSL certificate, and got the following message in the Pusherlog: "Notification error: APN invalid token".
我回去完全重新创建了我的 AppID、SSL 证书、规定等,但无济于事。接下来,我使用NWPusherMacOS Pusher应用程序尝试向 MyApp 发送推送通知。我将Pusher配置为使用 MyApp 的生产 SSL 证书发送推送通知(到MyApp的Ad Hoc分发的 tokenID ),并在Pusher日志中收到以下消息:“通知错误:APN 无效令牌”。
I next noticed that Pusherhad a checkbox option “Should use sandbox environment” checked. I unchecked the option and tried it again. This time the Push Notification was sent and then received by the target phone without any errors. This led me to take another look at the NWPusher.connectcall:
我接下来注意到Pusher有一个复选框选项“应该使用沙箱环境”被选中。我取消选中该选项并再次尝试。这次推送通知被发送,然后被目标手机接收,没有任何错误。这让我重新审视了NWPusher.connect调用:
Swift:
迅速:
let pusher:NWPusher? = try? NWPusher.connect(withPKCS12Data: pkcs12! as Data, password:APNS_Certificate_Password,environment:NWEnvironment(rawValue:1)!)
which has an environment argument NWEnvironment enum with possible values 0=none, 1=sandbox, 2=production, 3=auto.
它有一个环境参数 NWEnvironment 枚举,可能的值是 0=none、1=sandbox、2=production、3=auto。
Changing rawValue:1 (develop sandbox) to rawValue:2 (production) fixed the problem: Production Push Notifications now work without errors.
将 rawValue:1(开发沙箱)更改为 rawValue:2(生产)解决了这个问题:生产推送通知现在可以正常工作了。
回答by phantom_2
xCode 8.0
代码 8.0
I had "invalid token" error the after installing of xCode 8.0.
The issue was found here: Project Navigator -> select project (topmost item) -> select project name in Targets menu -> choose Capabilities in top menu -> look at PUSH Notifications section -> click "Fix issue"
安装 xCode 8.0 后出现“无效令牌”错误。问题在这里找到:项目导航器 -> 选择项目(最上面的项目) -> 在目标菜单中选择项目名称 -> 在顶部菜单中选择功能 -> 查看推送通知部分 -> 单击“修复问题”
回答by phil
Under ios App Bundle IDs menu in https://developer.apple.com/account/ios/identifier/bundle
在https://developer.apple.com/account/ios/identifier/bundle 中的ios App Bundle IDs 菜单下
click the id of your app if in production, add a new certificate if the old one has expired.
如果在生产中,请单击您的应用程序的 ID,如果旧证书已过期,请添加新证书。
Ensure your App Bundle ID com.myapp.mycom
matches your Certificate Name com.myapp.mycom
确保您的 App Bundle IDcom.myapp.mycom
与您的证书名称匹配com.myapp.mycom