Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the possible reasons to get APNs responses BadDeviceToken or Unregistered?

When sending notifications to iOS users, for some of them I get response status code 400 (BadDeviceToken) or code 410 (Unregistered).

From Apple documentation about "BadDeviceToken":

The specified device token was bad. Verify that the request contains a valid token and that the token matches the environment.

What is the meaning of "bad"? I know for a fact that the device token was valid at some earlier time. What does a user do to make its device token bad?

From documentation about "Unregistered":

The device token is inactive for the specified topic.

Does this necceserally mean that the app has been deleted? Or there can be some other reasons for this response.

like image 535
Shai Givati Avatar asked Feb 28 '17 14:02

Shai Givati


People also ask

What is APNs registration?

Apple Push Notification service (APNs) must know the address of a user's device before it can send notifications to that device. This address takes the form of a device token unique to both the device and your app.

Why are APNs used?

Apple Push Notification service (APNs) is a cloud service that allows approved third-party apps installed on Apple devices to send push notifications from a remote server to users over a secure connection. For example, a newstand app might use APNs to send a text alert to an iPhone user about a breaking news story.

How do I check my APN push notifications?

Sending a test Android push notificationClick on Settings and open Mobile Apps. Click on the Android App and make sure the Firebase API key has been configured. Click on Test Push and enter the device token for your test device. Add a test payload and send the test.


1 Answers

As you've quoted from Table 8-6 in the APNS documentation, there are two possible causes for the error:

  1. That the device token is invalid
  2. That the device token does not match the environment

If it is the first case, make sure that the iOS app registers the device for remote notifications every single time that the app is launched because there are many reasons for the device token to change across launches, as outlined in Configuring Remote Notification Support.

If it is the second case, you need to be sure that:

  • The backend uses development configurations if your app build was signed with development APNS entitlements, and
  • The backend uses production configurations if your app build was signed with production APNS entitlements.

Luckily, as the iOS developer, you don't need to directly change the APNS entitlements yourself. It is always in development, and is only automatically changed by Xcode to production when you generate the build and export for App Store or enterprise distribution. As for the backend, your backend developer should know how to configure the backend for development and production environments. For some frameworks, it is a matter of toggling some boolean named isProduction. Ultimately, according to Communicating with APNs under the section APNs Connections, push notifications are sent to different APNS endpoints depending on whether the environment is production or development.

Let's pretend that the BadDeviceToken error is due to the second case--that the device token registered by the app does not match the backend's properly configured development environment. First, in your Xcode project, check your .entitlements file and verify that the APS Environment key's value is development. It should look like this:

enter image description here

Next, after you generate an archive, open the Organizer (via the Window menu > Organizer), select the archive, and click on Export... at the right. You should see four methods of distribution:

enter image description here

If you select App Store or Enterprise, you will see in the later dialogs that Xcode changes the APNS entitlements to production (see tip of red arrow):

enter image description here

If you select Ad Hoc or Development, the text under aps-environment will be development, which should then match the backend's configurations.

like image 88
MLQ Avatar answered Oct 02 '22 05:10

MLQ