iOS: Production push notifications, Invalid token from APNS server
Asked Answered
R

6

18

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?

Resign answered 21/3, 2014 at 20:13 Comment(1)
#21424125Godolphin
R
21

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.

Resign answered 24/3, 2014 at 12:56 Comment(3)
If the the p12 don't match the token, the "invalid token" error has a certain logic.Olfe
Do you know if pushing an upgrade to the app store invalidates any push tokens associates w/ a previous version of the app?Cholecystotomy
I am not using certificates, I am using AuthKey file still getting this errorAcridine
A
18

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.

Ablaze answered 21/3, 2014 at 21:6 Comment(3)
I did remove the old token from the db, and I know it was correctly signed, so maybe it's storing an old token.Resign
@JamesHarpe If you're storing an old token in your app you should know about it, since it would be in your app's code.Ablaze
Ah, well I'm definitely not doing that.Resign
P
3

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.

Polyploid answered 13/7, 2017 at 21:18 Comment(1)
Thank you for the hint with "checkbox option 'Should use sandbox environment'" after uncheck this option, push could be send to the adhoc environment.Twitch
C
2

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

Cape answered 27/10, 2016 at 11:8 Comment(0)
L
0

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

Lowboy answered 11/4, 2016 at 11:7 Comment(1)
how did you figure out the .p12 had bad private keyPresto
I
0

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

Iodometry answered 3/8, 2018 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.