简体   繁体   English

iOS:生产推送通知,来自 APNS 服务器的无效令牌

[英]iOS: Production push notifications, Invalid token from APNS server

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.编辑:我没有在我的代码中存储令牌,Eran 的回答表明唯一的其他可能性是我的数据库中的旧令牌,或者应用程序没有被生产配置文件签名。 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.我清除了我的数据库,所以我知道它不是前者,至于后者,我不明白这是怎么回事,因为我只有一个分布配置文件,正如我上面所示,它正确设置了“aps-environment”键。 XCode wouldn't even let me use a development profile for app store submission, would it? XCode 甚至不会让我使用应用商店提交的开发配置文件,是吗? A few other possibilities:其他几种可能性:

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? AppID 中是否包含另一个用于推送通知的配置文件? Could a problem with that cause the invalid token error?是否有问题会导致无效令牌错误?

I re-downloaded the push production certificate and exported it from the keychain as .p12.我重新下载了推送生产证书并将其从钥匙串中导出为 .p12。 This seems to have solved the problem.这似乎解决了问题。 It seems strange though that a bad private key was giving me the "Invalid Token" error.尽管错误的私钥给了我“无效令牌”错误,但这似乎很奇怪。

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 索要当前令牌并仍然获得开发令牌)您的应用程序未使用生产配置文件进行签名。

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. MyApp 是一个 iOS 应用程序,它使用从每个 MyApp 在登录时保持更新的数据库中获取的 APN 令牌 ID 来发送和接收推送通知 MyApp 到 MyApp。 The Push Notifications are sent by MyApp using the NWPusher framework API.推送通知由 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.我回去完全重新创建了我的 AppID、SSL 证书、规定等,但无济于事。 Next I used the NWPusher MacOS Pusher app to experiment with sending Push Notifications to MyApp.接下来,我使用NWPusher MacOS Pusher应用程序尝试向 MyApp 发送推送通知。 I configured Pusher to send a Push Notification (to the tokenID of an Ad Hoc distribution of MyApp) with MyApp's Production SSL certificate, and got the following message in the Pusher log: " Notification error: APN invalid token ".我将Pusher配置为使用 MyApp 的生产 SSL 证书发送推送通知(到 MyApp 的Ad Hoc分发的 tokenID),并在Pusher日志中收到以下消息:“通知错误:APN 无效令牌”。

I next noticed that Pusher had a checkbox option “ Should use sandbox environment ” checked.我接下来注意到Pusher有一个复选框选项“应该使用沙箱环境”被选中。 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.connect call:这让我重新审视了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(生产)解决了这个问题:生产推送通知现在可以正常工作了。

xCode 8.0代码 8.0

I had "invalid token" error the after installing of xCode 8.0.安装 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"问题在这里找到:项目导航器 -> 选择项目(最上面的项目) -> 在目标菜单中选择项目名称 -> 在顶部菜单中选择功能 -> 查看推送通知部分 -> 单击“修复问题” 在此处输入图片说明

Found the root cause for this issue, it is because of bad private key.找到了这个问题的根本原因,是因为私钥不好。 To resolve this issue just generate a .p12 certificate with .pem .要解决此问题,只需生成带有 .pem 的 .p12 证书。 For creating the p12 file with .pem file please follow the below method要使用 .pem 文件创建 p12 文件,请按照以下方法操作要遵循的步骤

Under ios App Bundle IDs menu in https://developer.apple.com/account/ios/identifier/bundlehttps://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 ID com.myapp.mycom与您的证书名称com.myapp.mycom匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM