firebase-admin-java icon indicating copy to clipboard operation
firebase-admin-java copied to clipboard

Handle invalid token responses in FCM

Open yanikapitanov opened this issue 1 year ago • 3 comments

As per the documentation a token could be invalid if you have two error codes:

  • UNREGISTERED (HTTP 404)
  • INVALID_ARGUMENT (HTTP 400)

However INVALID_ARGUMENTS can be caused by other errors.

At the moment we receive the content below from the IncomingHttpResponse which is also the error message from the FirebaseMessagingException. What we do is to check the string of the message in order to delete these tokens.

Content: {
  "error": {
    "code": 400,
    "message": "The registration token is not a valid FCM registration token",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
        "errorCode": "INVALID_ARGUMENT"
      }
    ]
  }
}

What is the proper way to handle these errors? Does it make sense to update FirebaseMessagingException to provide more information about which field is invalid or perhaps have a new error code - INVALID_TOKEN? This would remove a lot of unambiguity as to when it is needed to manage the token.

yanikapitanov avatar Jan 24 '25 12:01 yanikapitanov

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Jan 24 '25 12:01 google-oss-bot

Could it be that mobile clients sent an ill-formed registration token? Could you check what was the token at the time of receiving this error?

tbadalov avatar Jun 02 '25 08:06 tbadalov

Is there no way to delete the token from Firebase using my server side API, so that when my client calls getToken a new token is generated?

My use case is for web push notifications. The web application is getting the token and sending it to my API which we store and use. I'm looking into how we can handle tokens that have expired.

johnmc-tc avatar Sep 12 '25 11:09 johnmc-tc