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.
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:
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?
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?
- iOS Push Notification custom format
- What Happen If a APNS Device Token Expired?
- Push notifications that trigger a background refresh before showing the push notification VS silent push
- How do I prevent Alert when App is on Foreground with Incoming OneSignal Push Notification?
- Using sandbox APNS on AdHoc Distribution Builds for iOS
- xcode 8.0 “add the Push Notif(..) to your App ID” warning, while already added
5 Solutions Collect From Internet About “iOS: Production push notifications, Invalid token from APNS server”
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.
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.
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
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”
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:
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.
- Apple Swift: how to get current seconds in 1/100 or 1/1000?
- On a jailbroken iPhone, how can I run commands as root?
- Blurring a UIPickerView's background
- Setting off a function in another class
- Swift Package Manager – UIKit Dependency
- iOS Custom UIImagePickerController Camera Crop to Square
- Rounded Corners only on Top of a UIView
- Why does this code cause “EXC_BAD_INSTRUCTION”?
- iOS – Unable to Upload media with Twitter/Fabric New SDK
- Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer – ios
- Autolayout and Scrollview width fixed to screen (not content)
- Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
- Xcode 6.1 using same 2x image for iPhone 6 and 4s simulator
- Pass variable to custom UITableViewCell from UIViewController
- Format date string with .000Z into NSDate