APN BadDeviceToken iff running dev version - ios

The problem
I'm not receiving Push Notifications to my app when I'm working on it,
and the APN server returns "BadDeviceToken".
The situation
I've got to be missing something simple here, this is the situation :
When I send a Push Notification to my app downloaded from the App Store it works
When I send a Push Notification from the same server, to the same version (*) of my app but built off Xcode, the APN server returns a status: '400' fail with the failed[0].response.reason: 'BadDeviceToken'
(*) I say the same version, the code is the same and the plugins should be the same, but I have changed Cordova iOS version down to 3.x, from 4.x.
The tech
I'm using the newer method of Push Notifications where on the server I just have a .p8 APNs Auth Key on the server.
The server is node code using the node-apn plugin.
The client app is Cordova, using the Telerik Push Plugin.
What I've tried
I've tried creating a new APNs Auth Key and uploading that to the server; that still works on the App Store version of the app, and APN still returns "BadDeviceToken" when sending to my locally built version of the app.
I've tried setting production: false on the server config for node-apn before sending the push to my locally built app; I still get the "BadDeviceToken" error.
Doing an extra hexadecimal encode to the token as per this question; result is that it still says "BadDeviceToken", though in that case it would actually be true.
Bumping the cordova-ios version back up to 4.3.1; still getting "BadDeviceToken".
Double checking that the "Push Notifications" Capability is on when building
Doing a Release build rather than a Debug build, as suggested here, and described here
The question(s)
Why is the APN server saying BadDeviceToken when that is obviously not true?
And, how can I fix it?

Deleting the app and reinstalling it (ie. re-running it from Xcode) gave a new token and the new token worked without complaint.

In addition to #kris's answer:
You could've probably gone solved it as well by running the app through xCode, that will generate a development build, and will need to use the APNS development gateway with a development certificate, not the production certificate.
Or if you are installing directly to your phone from Xcode, then you have to use Development security profile and make sure you are sending the notification from server side to iOS devlopment sand box. If you are using TestFlight/AppStore you need to use Production profile and make sure you are sending the notification to iOS production sandbox.
The SOLUTION however was found by the one that made the question itself, stating:
For some reason the Xcode project did´t use the right provision profile and the solution would be rather to re-install the app.
Further I'd like to explain what APNS_BAD_DEVICE_TOKEN is:
The specified device token was bad. Verify that you specified a valid token and that your Security Profile's type (development/production) matches your push certificate's type.

Related

Apple push notification for production in iOS not working

To start with the problem, the push notification certificates in the backend was set-up by another developer and was perfectly working fine. However, the developer has left and as I do not have access to his machine hence I created new distribution certificates to upload the new version of the app, which then stopped the push notifications to get delivered. I'm working with push notifications for the first time and quite confused where the problem lies. Can anyone give me a clear picture what is happening and how to go about it?
Here's what I did after it stopped working. I went to the developer portal, and as it was showing push notification was enabled hence,
generated a code signing request or CSR file in my mac
downloaded the aps.cer file
created .p12 and .pem file
After this, I have provided both the .p12 and .pem file to our backend guy, but still, it was not working on our test flight builds.
I then tested using Pusher application which was available from GitHub and it successfully delivers the push notification to the test flight build using production certificates.
Hence can anyone give me a clear idea, where I could have gone wrong?
Do I need to revoke my push notification SSL certificate and generate again? or what could be the possible mistake?
If it is working using any online tool such as pusher, then it should work fine from code as well. your developer is missing something, Ask him to check configuration properly. he must invoke production APNS url/port if it is production certificate and production app. he must invoke sandbox APNS url/port if it is sandbox certificate and sandbox app. Below url/ports are used for APNS(assuming you are using directly APNS instead firebase)
SANDBOX_GATEWAY_HOST = "gateway.sandbox.push.apple.com"
SANDBOX_GATEWAY_PORT = 2195
SANDBOX_FEEDBACK_HOST = "feedback.sandbox.push.apple.com"
SANDBOX_FEEDBACK_PORT = 2196
PRODUCTION_GATEWAY_HOST = "gateway.push.apple.com"
PRODUCTION_GATEWAY_PORT = 2195
PRODUCTION_FEEDBACK_HOST = "feedback.push.apple.com"
PRODUCTION_FEEDBACK_PORT = 2196
Also make sure these url are reachable from your server, try to send a push notification from server using curl command and debug if still issue persist. Keep in mind proxy also if you using.
Just answering here so that it can be helpful to someone else. Everything was set up as accordingly, however, the problem was 'Sandbox' key in the server was set to 'True' for even production. Once we turned it false, everything worked perfectly.

Testflight - Universal Push Notification Client SSL Certificate - BadDeviceToken

I'm to trying to test my push notifications while utilizing TestFlight.
I can successfully receive a push notification on a device when I deploy directly to it from Xcode. However when I deploy the app to the device via TestFlight (as an external tester) I get "BadDeviceToken".
I have created a "Universal Push Notification Client SSL Certificate" and followed these apple instructions to the letter (I think). As per link:
"The client SSL certificate that is generated is a universal certificate that allows your app to connect to both the development and production environments."
Has anyone got any ideas?
Thanks
FYI:
Relevant Application Service:
iOS Certificates:
ok, this was my silly error. I am using Pushy.
You specify the environment like so:
apnsClient.connect(ApnsClient.PRODUCTION_APNS_HOST);
Unfortunately I had specified DEVELOPMENT_APNS_HOST before. It would seem that when you use TestFlight you have to specify the production apns host.
Thanks for everyone that looked over the question and answered.
You should try to re-create the certificates from scratch. Make sure that the certificates are production/distribution and not using APNS sandbox. Also, make sure it's on an actual approved device and not a simulator.
From the APNS Docs:
The specified device token was bad. Verify that the request contains
a valid token and that the token matches the environment
Also, check out these two links (first one is old):
iPhone APNS Device Tokens in sandbox vs. production
https://salesforce.stackexchange.com/questions/119615/send-push-notifications-to-ios-using-apns

Push notifications not received via TestFlight

My app is currently being tested by testers via TestFlight. The problem is that they don’t receive any push notifications sent. It’s working on my own device when connecting it to my mac and running the app directly from x-code.
I feel like I’ve tried almost everything, from changing certificate from dev to production to recreating a new production APNS certificate and build using a new profile referencing it, etc.
I know this question has been discussed on other topics but I never found the right answer in my case. I can't post images here because I don't have enough reputation but I have relevant screenshots.
Any help is welcome :)
Elements to help diagnostic :
It’s working on my device when I run the app directly from x-code.
The testers are asked properly if they want to authorize push notifications on their devices (at least the first install, after if they want to get asked again they have to do what’s described in Reset push notification settings for app)
The registering is working because I can see their devices tokens in the logs (but I have the feeling that the token is always the same, no matter if the app is built with the production or the dev certificate, not sure if it’s normal).
I have an APNS Production certificate.
I have a distribution profile, with “push notification” enabled. It’s linked with my “full” appID (and not the generic one with “*”).
I’m signin the build in release mode with the distribution certificate (not in debug mode), and I’m using the provisionning profile described above (the distribution one, linked with my app id)
And when I check in the "binary & entitlements" window just before submitting the build I see the the provisionning profile is correct and that "aps-environment" is set to "production".
Server-side : The APNS call is made by using javapns (https://code.google.com/p/javapns/) from a google app engine server. Like I said the notification are correctly received on my own device so I guess this call is working. We have a .p12 file generated from the APNS developpement certificate and a .p12 file from the APNS production certificate, and I've tried to make the call with both and it's not working. The server-side code hasn't change from the previous version and it was working in production with the app on the store. The problem is really focused on testing with testflight.
Thank you all !
If you followed Ray Wenderlich's tutorial and you made everything work in development mode, the issue most likely comes the fact that you now need to create a new .pem-file for production mode.
So, you need to perform exactly the same steps that you did with the development certificate (i.e. the .p12-file) you downloaded once more with the production certificate which you can obtain in the iOS dev center as well:

Push notifications not coming when we download the build from testflight or diawi iOS

I have successfully created the .pem and .p12 file by
http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
Tutorial.
We are using java as a server so I also exported my .p12 key using this link as there was an error of directly exporting the .p12 file
Cannot send push notifications using Javapns/Javaapns SSL handshake failure.
Everything is working great in debug mode. Whenever I install a build by xcode I receive the push notifications successfully everytime. But after archiving and uploading the build on testflight or diawi and then installing it on the device I didn't get any push notifications. I am successfully getting the device token from apns server. On debugging at server side we found that the device token they receive and send the notifications is invalid. This response is sent by the apns server to our java server. . I am using Xcode 6.0 or later and my app is compatible with ios7.0 or later. And yes I have made checks in registering for remote notifications for ios 8 and 7.Has anyone has faced this issue because earlier in xcode 5 series this hasn't happen. Please help
Any help will be appreciated. Thanks
If you followed Ray Wenderlich's tutorial and you made everything work in development mode, the issue most likely comes the fact that you now need to create a new .pem-file for production mode.
So, you need to perform exactly the same steps that you did with the development certificate (i.e. the .p12-file) you downloaded once more with the production certificate which you can obtain in the iOS dev center as well:
Here is the quote from the tutorial that you probably overread:
If you’re ready to release your app to the public, you will have to
repeat this process to make an Ad Hoc or App Store distribution
profile.
1.)Check if the pem file you have stored on your server is created by using a development certificate or distribution certificate, for push notifications to work through ttestflight you need to create a pem file in the distribution mode,
2.) also check if the gateway you are delivering to is gateway.push.apple.com:2195, and not the sandbox one,
3.)the notification tokens are also different for development and distribution profile, check all these scenarios and see
In order for push to work you have to use the exact same AppID for both creating APNs cert and Provisioning Profile. Wildcard profiles cannot be used for push notifications. May this be your problem(using wildcard or just different AppID for TestFlight/diawi)?

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?
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?
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.
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.
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.
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"
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
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

Resources