FCM token not generating in Samsung devices in release version of app

青春壹個敷衍的年華 提交于 2021-02-08 07:19:34

问题


I am using FCM for sending notifications in my app. The first version of my app is in the testing phase and there is an error in Samsung devices because FCM token is not getting generated in these devices. Here is my code which generates the token:

FirebaseInstanceId.getInstance().instanceId.addOnCompleteListener(OnCompleteListener { task ->
                    if (!task.isSuccessful) {
                        return@OnCompleteListener
                    }

                    // Get new Instance ID token
                    val token = task.result?.token

                    val ref = FirebaseDatabase.getInstance().getReference("/users/$uid")
                    ref.child("Message Token").setValue(token)

                })

I have also kept a check in my SplashActivity for Google Play Version. Here is the code:

private fun checkPlayServices() {
    val apiAvailability = GoogleApiAvailability.getInstance()
    val resultCode = apiAvailability.isGooglePlayServicesAvailable(this)
    if (resultCode != ConnectionResult.SUCCESS)
    {
        if (apiAvailability.isUserResolvableError(resultCode))
        {
            val dialog: Dialog = apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
            dialog.setCancelable(false)
            dialog.setCanceledOnTouchOutside(false)
            dialog.show()
        }
        else
        {
            Log.d("Demo App", "This device is not supported.")
            val dialogApp = AlertDialog.Builder(this@SplashActivity)
            dialogApp.setTitle("Error")
            dialogApp.setMessage("Some features in the app may not work in your device. Download latest version of play services?")
            dialogApp.setCancelable(false)
            dialogApp.setPositiveButton("OK"
            ) { _, _ ->
                GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(this).addOnSuccessListener { Toast.makeText(this@SplashActivity,"Google Play Services Updated. Please Logout and Re-Login", Toast.LENGTH_LONG).show() }
                    .addOnFailureListener { Toast.makeText(this@SplashActivity,"Could not update Google Play Services", Toast.LENGTH_LONG).show() }
            }.setNegativeButton("No"){_, _ ->
            }
            dialogApp.show()
        }

    }
}

To add to the above. When I tested in Samsung real devices in the debug version as well in Emulators the token was correctly generated. In fact there is one Samsung device where I had installed the app directly from Android Studio. It worked fine over here. Later on I tried to install the app in this device from Play Store. But there was a problem caused in PlayStore and there was no downloading happening after this. I had to restore PlayStore to factory settings. Post this, PlayStore started behaving normally. But token was not generated.

Any help would be highly appreciated.


回答1:


The problem is of Proguard. While there is no direct way of handling this, I could find a workaround. In the proguard-rules.pro file I added the following line:

-keep class com.google.firebase.** { *; }

This is to make sure that firebase classes are not deleted by Firebase. Things as of now are working properly even in Samsung devices



来源:https://stackoverflow.com/questions/59852062/fcm-token-not-generating-in-samsung-devices-in-release-version-of-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!