0

I am following this tutorial for cloud messaging subscription, and I am running into an issue. I can't import the right Message class i need. I found that i probably missed a dependency, I found I needed this one:

implementation 'com.google.firebase:firebase-admin:6.9.0'

With this dependency the Message class is no longer a problem, however now some stuff changes in my subscribe function:

private fun subscribe(){
        val list = mutableListOf<String>()
        list.add("dufD62vvRHuL_xZ9ROHTod:APA91bEJJNVPu3lyVbYCcUJqz-5vihXCCBJhtKAohi28CZNDvwb9HsdYCdICFRCjkNaRggr47igapLYEnlEEzN7Mk1ClZoUVT3VdxB6PVBAAa6u0yylo1NojLTLpUDrC8tMqTsBdlKAe\n")
        Firebase.messaging.subscribeToTopic("weather")
                .addOnCompleteListener { task ->
                    var msg :String= "Subscribed successfully"
                    if (!task.isSuccessful) {
                        msg = "Subcription failed"
                    }
                    Log.d("TAG", msg)
                    Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
                }
    }

Right now the subscribeToTopic also expects a list of tokens and i am unable to add a onCompleteListener.

In addition a function I made previously suddenly gets errors

private fun getToken(){
        FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
            if (!task.isSuccessful) {
                Log.w("TAG", "Fetching FCM registration token failed", task.exception)
                return@OnCompleteListener
            }

            // Get new FCM registration token
            val token = task.result

            // Log and toast
            val msg = token.toString()
            Log.d("TAG", msg)
            Toast.makeText(baseContext, msg, Toast.LENGTH_LONG).show()
        })
    }

It seems like the admin dependency messes with the original code.

Is there someone who can help me with this? It would be appreciated.

yourivdloo
  • 364
  • 2
  • 16
  • The admin dependency is not meant to be used in your app, you are actually opening yourself to a possible security breach since you are now exposing your Firebase keys in your app allowing basically anyone who gets their had on your key to send push messages on their behalf. the admin dependency is for servers so they can send messages to devices – tyczj Apr 23 '21 at 12:49
  • *firebaser here* Calls to the FCM REST API to **send** messages can only be done from a trusted environment. The reason for this is that this API can send whatever message they want to all of your users. If this was possible from your Android app, a malicious user could use it and you're putting your users at risk. See https://stackoverflow.com/a/37993724 for a better solution. – Frank van Puffelen Apr 23 '21 at 14:39

0 Answers0