iOS: Production push notifications, Invalid token

2019-01-17 21:55发布

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.

However, I'm now getting an Invalid Token error back from the APNS servers.

I have the Archive scheme set to "Release", and I have Release set to use this distribution profile:

enter image description here

Inside that provisioning file, you can see that I have the environment set correctly:

enter image description here

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:

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?

6条回答
劳资没心,怎么记你
2楼-- · 2019-01-17 21:56

xCode 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" enter image description here

查看更多
乱世女痞
3楼-- · 2019-01-17 22:00

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.

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.

查看更多
欢心
4楼-- · 2019-01-17 22:06

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 . For creating the p12 file with .pem file please follow the below method Steps to follow

查看更多
三岁会撩人
5楼-- · 2019-01-17 22:11

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 NWPusher framework 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 NWPusher MacOS Pusher app to experiment with sending Push Notifications to 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".

I next noticed that Pusher had 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.connect call:

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.

Changing rawValue:1 (develop sandbox) to rawValue:2 (production) fixed the problem: Production Push Notifications now work without errors.

查看更多
迷人小祖宗
6楼-- · 2019-01-17 22:17

Under ios App Bundle IDs menu in https://developer.apple.com/account/ios/identifier/bundle

click the id of your app if in production, add a new certificate if the old one has expired.

Ensure your App Bundle ID com.myapp.mycom matches your Certificate Name com.myapp.mycom

查看更多
Explosion°爆炸
7楼-- · 2019-01-17 22:20

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.

查看更多
登录 后发表回答