firebase / firebase-android-sdk

Firebase Android SDK
https://firebase.google.com
Apache License 2.0
2.25k stars 571 forks source link

FirebaseMessaging : Topic sync or token retrieval failed on hard failure exceptions. #5801

Closed dev-shushant closed 5 months ago

dev-shushant commented 5 months ago

Environment

Steps to reproduce:

We have found that Firebase Token is not generated at device after analyzing the logs internally in the team even though FirebaseApp Installation was successful. We saw an error as below:

 FirebaseMessaging   <package-name> E  Topic sync or token retrieval failed on hard failure exceptions: java.util.concurrent.ExecutionException: java.io.IOException: TOO_MANY_REGISTRATIONS. Won't retry the operation.

We have dug into the Firebase SDK code and we were able to reach till the below code:


@Override
@SuppressLint("WakelockTimeout") // WakeLock will always be released
public void run() {
  if (ServiceStarter.getInstance().hasWakeLockPermission(getContext())) {
    syncWakeLock.acquire();
  }
  try {
    // this should have already been set to true, do it again for safety.
    firebaseMessaging.setSyncScheduledOrRunning(true);
    if (!firebaseMessaging.isGmsCorePresent()) {
      firebaseMessaging.setSyncScheduledOrRunning(false);
      return;
    }
    if (ServiceStarter.getInstance().hasAccessNetworkStatePermission(getContext())) {
      if (!isDeviceConnected()) {
        ConnectivityChangeReceiver receiver = new ConnectivityChangeReceiver(this);
        receiver.registerReceiver();
        return; // return and wait for connectivity
      }
    }
    if (maybeRefreshToken()) {
      firebaseMessaging.setSyncScheduledOrRunning(false);
    } else {
      firebaseMessaging.syncWithDelaySecondsInternal(nextDelaySeconds);
    }
  } catch (IOException e) {
    Log.e(
        TAG,
        "Topic sync or token retrieval failed on hard failure exceptions: "
            + e.getMessage()
            + ". Won't retry the operation.");
    firebaseMessaging.setSyncScheduledOrRunning(false);
  } finally {
    if (ServiceStarter.getInstance().hasWakeLockPermission(getContext())) {
      syncWakeLock.release();
    }
  }
}
google-oss-bot commented 5 months ago

I found a few problems with this issue:

lehcar09 commented 5 months ago

Hi @dev-shushant, thank you for reaching out. TOO_MANY_REGISTRATION issue usually occurs in cases:

  1. When a client has a multitude of apps registered with FCM, this would trigger the TOO_MANY_REGISTRATIONS error. The problem here is, the only mitigation is for the user to delete some of the other client apps from the device to make room for the new one.
  2. Otherwise, the device is likely "cloned" from another one, so that multiple devices have the same identifiers. In this case, the best thing is factory-reset.

It would be best for you to reach out to our Firebase support for better assistance. You'll be able to share your project details and configuration which will allow them to support you better. Thanks!