68

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.

2

6 Answers 6

97

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.

4
  • 3
    You saved my sanity! Thank you! Oct 21, 2018 at 11:24
  • 2
    In my own case, I had to generate a production APN certificate using this link - customersupport.doubledutch.me/hc/en-us/articles/… and also set the production value of the ios push config to true
    – hassan
    Apr 3, 2020 at 8:33
  • Than you for this detailed answer. It really saved me a lot of time.
    – Jeff S.
    Apr 10, 2020 at 9:21
  • Just wanted to add that in my case the push library I was using would send it to the development APNs endpoint, instead of the production one, because the environment had DEBUG set. This answer pointed me in the right direction.
    – BooTooMany
    Jun 3, 2021 at 2:25
21

I was sending a "development" device token to the "production" apple push servers. I fixed it by sending requests to api.development.push.apple.com instead of api.push.apple.com

5
  • Thank you a lot.Saved me 3 days of burning. I had created a build for testers through the TestFlight in iOS and i had the oposite thinking that my app was still in development and sending the PN in the wrong apple servers.
    – theocikos
    Nov 13, 2020 at 15:25
  • Where did you get api.development.push.apple.com i have found only api.sandbox.push.apple.com in developer.apple.com/documentation/usernotifications/… Feb 18, 2022 at 14:03
  • @VaclavStengl perhaps they updated it? My answer is from two years ago in a codebase that is no longer in use. Which URL(s) have been working for you?
    – raphaelrk
    Feb 19, 2022 at 4:35
  • 2
    Those 2 work now: Development server: api.sandbox.push.apple.com:443 Production server: api.push.apple.com:443 Feb 20, 2022 at 20:29
  • 2
    @VaclavStengl for the record, api.development.push.apple.com also still works! Jan 5 at 7:04
13

Status code '400' : You get this error when you try to send the notification with a wrong certificate. Make sure that you use production certificate for production environment. It is bad because you are using bad configurations.

Status code '410' : Yes with this code we can understand that App has deleted. In our app when we get this status code we delete this token from db. The other scenario could be that user has reinstalled the app which could change his token. So its better you remove this token.

2
  • 3
    Can you get 410 if user just disabled push notifications?
    – DataGreed
    Feb 13, 2018 at 18:20
  • 1
    @DataGreed, no. A push notification is delivered to the device, also if the user disabled the display of push notifications.
    – Manuel
    Jun 4, 2018 at 13:36
2

Error code 404: BadDevice token

Posssible reasons:

  1. Your .pem certificate may be wrong.
  2. Your BundleId may be wrong.
  3. Your Device id may be wrong.

Note: Append .voip with your bundleid for sending voip push notification(exmple: bundleid.voip)

Here is an workable example of voip push notification :

<?php
$token = $_REQUEST['tok'];
if (!defined('CURL_HTTP_VERSION_2_0')) {
  define('CURL_HTTP_VERSION_2_0', 3);
}
// open connection 
$http2ch = curl_init();
curl_setopt($http2ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
// send push
$apple_cert = 'certificate_name.pem';
$message = '{"aps":{"action":"message","title":"your_title","body":"your_message_body"}}';
$http2_server = 'https://api.development.push.apple.com'; // or 'api.push.apple.com' if production
$app_bundle_id = 'your bundle id';
$status = sendHTTP2Push($http2ch, $http2_server, $apple_cert, $app_bundle_id, $message, $token);
echo $status;
// close connection
curl_close($http2ch);
function sendHTTP2Push($http2ch, $http2_server, $apple_cert, $app_bundle_id, $message, $token) 
{
    // url (endpoint)
    $url = "{$http2_server}/3/device/{$token}";
    $cert = realpath($apple_cert);
    // headers
    $headers = array(
        "apns-topic: {$app_bundle_id}",
        "User-Agent: My Sender"
    );
    curl_setopt_array($http2ch, array(
        CURLOPT_URL => $url,
        CURLOPT_PORT => 443,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POST => TRUE,
        CURLOPT_POSTFIELDS => $message,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSLCERT => $cert,
        CURLOPT_HEADER => 1
    ));
    $result = curl_exec($http2ch);
    if ($result === FALSE) {
      throw new Exception("Curl failed: " .  curl_error($http2ch));
    }
    // get response
    $status = curl_getinfo($http2ch, CURLINFO_HTTP_CODE);
    if($status=="200")
    echo "SENT|NA";
    else
    echo "FAILED|$status";
}
?> 
0
2

If using node-apn. I found the answers here rather confusing as APN has moved to using tokens which work for either sandbox or production modes. I was also confused because writing a 1 off script to send a notification worked in production.

It wasn't until I started to suspect that my my service wasn't getting process.env.NODE_ENV === 'production'. So I added that to my startup log message and low and behold my service runner forever was not getting any environment variables. Because of this, it was trying production device ids on the sandbox url.

2
  • hey Kyle, I think I might be in the same place as you were back in June! Could you explain a bit more about what you mean by "was not getting any environment variables" I'm not sure I follow. cheers Dec 23, 2019 at 15:57
  • See github.com/node-apn/node-apn/blob/… about NODE_ENV. So I added NODE_ENV=production /usr/bin/forever <...> to my start up script. Dec 23, 2019 at 17:33
0

Feedback is no longer immediate with 410 notifications. Apple intentionally has done this due to privacy concerns. It may take up to a week now. See https://developer.ibm.com/customer-engagement/2019/05/01/apns-feedback-service-change/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.