firebase / firebase-android-sdk

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

IOException SERVICE_NOT_AVAILABLE FirebaseMessaging #4053

Open claucookie opened 2 years ago

claucookie commented 2 years ago

[READ] Step 1: Are you in the right place?

Issues filed here should be about bugs in the code in this repository. If you have a general question, need help debugging, or fall into some other category use one of these other channels:

[REQUIRED] Step 2: Describe your environment

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

Hi! We are experiencing a crash when trying to get the token for Firebase Cloud Messaging. This is happening in production, for some users only, Samsung and Nokia devices, but we are not able to reproduce it when downloading the app on our side or deploy from Android Studio.

The stacktrace is as follows:

Caused by java.io.IOException: java.util.concurrent.ExecutionException: java.io.IOException: SERVICE_NOT_AVAILABLE
       at com.google.firebase.messaging.FirebaseMessaging.blockingGetToken(FirebaseMessaging.java:620)
       at com.google.firebase.messaging.FirebaseMessaging.lambda$getToken$4(FirebaseMessaging.java:387)
       at com.google.firebase.messaging.FirebaseMessaging$$InternalSyntheticLambda$0$2fd74daaa9547626a2178c157d189c0e094dccccb61fc5ee6d886ccc44d7a00a$0.run$bridge(FirebaseMessaging.java:22)
       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
       at java.util.concurrent.FutureTask.run(FutureTask.java:266)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement@@18.0.0:2)
       at java.lang.Thread.run(Thread.java:919)

We know the Devices crashing contains Google Play Services, as we have them in house for testing as well. What other reasons could throw SERVICE_NOT_AVAILABLE?

Relevant Code:

We use a particular setup. Apart from our default Firebase Project, we have a 3rd party restriction to connect to a secondary Firebase project to receive push notifications (FCM).

// Initialization Code for the secondary Firebase Project used to receive push notifications.
Firebase.initialize(
            context = appContext,
            FirebaseOptions.Builder()
                .setApiKey(apiKey)
                .setProjectId(projectId)
                .setApplicationId(applicationId)
                .setGcmSenderId(senderId)
                .build(),
            INSTANCE_KEY
        )
        val firebaseApp = FirebaseApp.getInstance(INSTANCE_KEY)
        val firebaseMessaging = firebaseApp.get(FirebaseMessaging::class.java) as FirebaseMessaging
        Preconditions.checkNotNull(firebaseMessaging, "Firebase Messaging component is not present")

// The code trying to obtain the token
firebaseMessaging.token.addOnCompleteListener { task ->
            if (!task.isSuccessful) {
                continuation.resume(Result.failure())
            } else {
                continuation.resume(Result.success())
            }
        }

Any help is more than welcome.

google-oss-bot commented 2 years ago

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

argzdev commented 2 years ago

Thanks for reporting, @claucookie. The SERVICE_NOT_AVAILABLE can happen for the following possible reasons:

  1. The phone is offline.
  2. The phone is online but it cannot reach Google servers.
  3. The firebase messaging servers are temporary unavailable.
  4. Google Play services is in an extremely bad state. ~5. Google Play services is not installed on the phone.~ (Since you've mentioned the crashing devices contains Google Play)

How many % of your DAU are experiencing the crash? Could it be due to intermittent internet connection by the users?

Just some notes here: I wonder if this could be related to #3987, however, it might not be since you're not using any multi sender processes.

claucookie commented 2 years ago

Thanks @argzdev for your quick response. Regarding your question:

Could you confirm if having multiple projects setup is still supported? In the related issue it is mentioned that some features might not be supported and there is not documentation around it, so I wonder if this could be also happening as well.

argzdev commented 2 years ago

Could you confirm if having multiple projects setup is still supported?

Let me consult with our engineers about that and get back to you.

As for the documentations, we apologize for that. We're still working with our technical writer to update the public documentations.

claucookie commented 2 years ago

Thanks @argzdev , looking forward to your response :)

argzdev commented 1 year ago

Sorry for the delayed response, @claucookie. I've communicated with our engineer. It was confirmed that due to the migration to FIS, any multiple project setup related to FCM may not work properly, including multi-sender setup.

Though that said, what we can do is mark this as a feature request, and our engineers may consider discussing this further in the future. If it's alright with you, could you share what use case do you have for setting up two firebase project for one app? It may help our engineers consider why we need multiple project setup for FCM. Thanks in advance!

claucookie commented 1 year ago

Thanks @argzdev, Im fine with feature request label and happy to answer any questions from the engineers when looking into this.

The use case comes from a Third Party restriction. We had Firebase already setup in our android app for a long time ago, and our server was sending us push notifications successfully via FCM (project 1). However we recently had to include a 3rd party library (customer care library with push notifications feature) which account was linked already to another FCM account (project 2) and couldn't be changed.

That's why we had to add a secondary FCM project setup only to receive push notifications inside that third party library.

argzdev commented 1 year ago

That does sound like a reasonable use case. Thank you for sharing, @claucookie!

si-ankush-yadav commented 1 year ago

Hi @argzdev @claucookie we are facing the same issue and I did able to replicate the crash on older device.

turns out when addOnCompleteListener is throwing ClassNotFoundException Exception on API level less than 23. Hope this help and we can come up with solution.

argzdev commented 1 year ago

Hi @ankush-yadav1, we've had discussions with our engineers, and it was communicated that since FirebaseApp.get() is annotated with @KeepForSdk.

@KeepForSDK marks methods in the SDK that are not part of the public API, and should only be used by other Firebase SDKs. There are no guarantees on how this functionality will be maintained; it can be kept or removed or changed as the need arises.

So it is no longer recommended to use this. That said, I've confirmed with our engineers that the recommended alternative solution for now is using firebase-iid, as long as the most recent versions of firebase-messaging and firebase-iid is used.

filipatbnp commented 1 year ago

@argzdev who are "our engineers"?

argzdev commented 1 year ago

Hi @filipatbnp, our engineers would pertain to Firebase product engineers. In this case, specifically, our Firebase Cloud Messaging and Firebase Core team engineers.