Aditya Dixit
Aditya Dixit

Reputation: 161

How to fix SERVICE_NOT_AVAILABLE error in firebase cloud messaging while getting token

I am trying to get fcm token and store it in cloud firestore:

val user = User(et_email_sign_up_activity.text.toString(), et_name_sign_up_activity.text.toString(),
                            et_mobile_sign_up_activity.text.toString(), "",
                        FirebaseAuth.getInstance().currentUser?.uid!!, tv_address.text!!.toString(), getToken())
                    Firebase().uploadUserSignIn(user, this)//uploads user to cloud firestore

getToken():

private fun getToken(): String{
        var token = ""
        FirebaseMessaging.getInstance().token.addOnSuccessListener {
            token = it
            Log.i("token", it)
        }.addOnFailureListener {
            val dialog = AlertDialog.Builder(this)
            dialog.setMessage(it.toString())
            dialog.show()
        }
        return token
    }

I always get this error whenever I try to execute my code in real android device:

E/FirebaseMessaging: Topic sync or token retrieval failed on hard
failure exceptions: java.util.concurrent.ExecutionException:
java.io.IOException: SERVICE_NOT_AVAILABLE. Won't retry the operation.

I have google play services running with proper internet connection

Few solutions I have tried:

  1. Add internet permission in manifest:
  2. Download google.json file from firebase after entering sha1 and sha256
  3. Enable android device verification in google cloud.

Sometimes I don't get the error and fcm token is returned successfully returned but most of the times I get this error.

What is the error?

Upvotes: 11

Views: 19857

Answers (1)

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16086

Even I am facing same problem, this issue comes when your device is not able to communicate with FCM due to some reason. You can perform following checks to make it working:

  1. Check your internet connecting with device
  2. Switch between mobile data to wifi or vice versa
  3. Device date time should be correct.
  4. Try cleaning cache.
  5. Kill the application and start it again.

Upvotes: 15

Related Questions