iOS Push device tokens getting always BadDeviceToken - ios

I hope I find a person who can help me. I have a iOS App in the App Store and the push device tokens I get, are wrong. Over the APNS Server (production and sandbox) I always get the answer: BadDeviceToken.
The app is developed with cordova, ionic and I use the phonegap push plugin.
When I deploy the app in debug mode directly to my iPhone, I get a token that is valid in sandbox and production.
But all tokens I actually get from iOS are all wrong.

Related

Push notification problems with Back4App and APN Authentication Key

I'm using ParseServer with Back4App for an Android and iOS App. I implemented a Cloud Function on the server to send push notifications through Parse and it works for the Android app without any issues (for both debug and production apks).
With the iOS Version I am currently running into the following problem:
I created an APN Authentication Key that I uploaded to Back4App. In the process I indicated that this key is for a debug app, and sending push notifications via the Cloud Function to my iOS debug device works like a charm, no matter what device triggers the Cloud Function. This was sufficient for development.
I need to be able to get push notifications to both device types to troubleshoot together with my TestFlight testers in an open test I recently started.
For all TestFlight test devices, push notifications are not getting delivered - which is not surprising, given that the APN Key was uploaded to Back4App for debug apps only.
I thus reuploaded the APN Key and indicated that it is a production app, and now sending push notifications to the TestFlight testers works, but sending push notifications to my iOS debug test device does not.
I tried getting around this by changing the build scheme in Xcode from Debug to Release - but with a release version on my iOS debug device, the push notifications are still not being delivered to said device. I can see in my Parse Dashboard that they are being sent, but they are not getting delivered.
I am using a Notification Service Extension and set the Build Mode to Release there as well, but to no effect.
Changing the APN Authentication Key on Back4App back to Debug only allows me to receive push notifications again on my debug device.
I tried changing the APN Authentication Key (for either debug and production, since I cannot select both on Back4App) before force quitting the app, restarting it to get a new APN token saved to the ParseServer, and then sending the test notifications, also to no avail. I tried this for both my debug device and a TestFlight device, and it is always either my debug device (who gets the app straight from Xcode) or the TestFlight devices that are able to receive push notifications, never both.
I did not get around to trying it with APN Certificates for which I read that there are two distinct certificates for Production and Debug apps. While this option would be possible with Back4App, one big question remains - how do you implement push notifications both for debug and production apps in iOS with only one APN Authentication Key? And more specifically, how do you do that on Back4App?
Or are there any other ways around this issue besides the old APN Certificates? Since APN Authentication Keys are made out to be the future I'd rather implement it with that instead of the older APN Certificates.
Any help is appreciated, thanks in advance!
I found a workaround and what caused the issue:
Using APN Authentication Keys for push notifications is easier to implement and more convenient since keys don't expire, can be used across multiple apps and do not require the generation of independent development and production certificates.
But: An APN Authentication Key can only be used for EITHER development push notifications OR production push notifications (which is apparently intended by Apple).
No matter whether build settings are set to Release or Debug, a tethered physical device that gets any build straight from Xcode will always have the APN environment flag set to development - even manually setting the APN environment flag to production before building does not have an effect, it seems to be reset at build time and defaults to development. This means that my debug test device (physically connected to Xcode) can only receive development pushes, which explained my described issues.
When archiving a build and uploading it to TestFlight, the APN environment flag is automatically set to production; all TestFlight testers thus only receive production push notifications.
What I am doing to work around this is that I registered my debug test device as a TestFlight tester so that I can download my latest build from there and take advantage of production pushes. I registered this particular test device as its own group so that I can test builds only with that device that I do not want to immediately roll out to all other TestFlight users in my second (true) group of testers.
Does that mean I always need to archive any new builds and push it to my test device via TestFlight to try something new?
No, only for things that require push notifications. For everything else I still get the latest build straight from Xcode because all locally stored data (like a logged-in Parse User or Installation data) persists on my device, no matter whether I install a new version from Xcode directly or through TestFlight automated updates.
That way I can work as usual and only have to take the additional TestFlight step for any push notification-related features.

Is it possible to use same bundle ID for development and distribution in ios?

I am using onesignal plugin for push notifications in my android and ios app so I want to test push notifications first on development side and then I will upload it on ios app store. Or is there any best practice for implementation of onesignal push notification first for testing and then distribution.
Personal opinion, you do not need separate profiles/certificates for development and production anymore in case of APNs. Recently apple has made some changes. Now you only need one '.p8' push notification certificate from your 'developer.apple.com' account. You give this .p8 file to your backend team.
Xcode will automatically manage your production and development on your side.
This is what you have to do to make it work:
For the backend:
In your app delegate, inside the function where you get APNs token from apple. This APNs token will either be development or production. How does apple decide, which one to give? Well when you export an IPA/App locally, or upload it on test flight or on App store, Xcode will automatically give you production push notification APNs. When you install IPA/App locally using xcode to a phone connected to xcode or on simulator, Xcode will automatically give you development/staing push notification APNs token inside that function. And at that point you just have to send that token to your backend.
How will backend check if the user's token they are getting is production or development?
Using your URL, if it is staging, it will consider it as development APNs token.
If it is production URL, it will consider it as production APNs token.
What you have to make sure?
Make sure the Base URL is set to production if you are uploading app to test flight.
Make sure the Base URL is set to development if you are running app locally on simulator or your phone connected to your xcode.

Amazon SNS with Firebase iOS

I have integrated AWS SNS with Firebase to send Push Notifications to iOS and Android.
Everything works fine for Android, but iOS is giving me headaches. On my development device (iPhone SE), running in development mode, the notifications are always received. But when the app is installed via App Store (production), the notifications never show up, on any device. Also, after trying to send a push, the SNS Endpoint gets disabled, and for the next attempts to send it, i get the error Endpoint Disabled. I have already checked the certificates and everything seems ok.
App iOS is configured at firebase with the APNS Auth Key (.p8)
Notifications sent via Firebase Message Test are delivered, what makes me think that the problem is not with apple certificates.
The delivery status log says
{ ... error: NotRegistered }
Finally figured out the solution.
I did not know that, after updating the certificates and keys in the app store developer, I had to build a new version of the app for applying these updates.
So, I reviewed all the info related to the certificates and the push notifications. When I was sure that everything was correctly configured, I build the IPA and uploaded it for Test Flight.
Now its working as it should =)

push notifications stopped working in app after cert update

My cert for push notifications expired a few days ago. I updated it on the developer portal and put the new one for my server to use. Everything works not for the app that is downloaded from the app store.
When i try to load a in-dev build from x-code all push notifications fail. Apple responds back with message "Invalid Token".
I have literally done nothing. I just started it up and started testing to make sure everything is working. Is there some way to update the cert in the app or something of that sort??
NOTE: uploaded my app to testflight and everything works. Only does not work while developing right from xcode.
For developing you use different APN and Certificate that for
Production.
Apple generate different tokens for app in Debug and app
in Production.
Your server is using a Production certificate and a
Production APN.
TestFlight app is a Production ready app.
Now:
In Debug you send a Debug token to a server in Production, that Debug token is NOT registered on Apple Production APN.
In TestFlight you send a Production token to a server in Production, that Production token IS registered on Apple Production APN.
So all is working as intended.

Can't get device token when distribution

When in development, it works well, I can push notification to device.
but when distribution I can't get notification.
After debug I found the reason is that my server didn't receive device token from app.
I'm sure I used the correct provisioning and enabled the apps for both development and production.
anybody knows what's the reason that app can't get device token?

Resources