Search code examples
androidfirebase-cloud-messaginggoogle-cloud-messagingandroid-library

FCM version update for SDK


FCM version: 21.0.0 :
I was using FirebaseInstanceId along with FirebaseOptionsBuilder to receive notifcation on SDK side of the project.

Current docs say Apps still using deprecated Instance ID APIs for token management should update all token logic to use the FCM APIs described here

So after migrating to 24.0.1, it seems my project require google-service.json to receive cloud messaging. My question is why do we have to add this google-services.json file if we can fetching the details from the server and set it like below as per previous version.

 val options = FirebaseOptions.Builder()
                .setGcmSenderId(firebaseConfig!!.GcmSenderId)
                .setApiKey(firebaseConfig.ApiKey)
                .setApplicationId(firebaseConfig.ApplicationId)
                .setStorageBucket(firebaseConfig.StorageBucket)
                .setProjectId(firebaseConfig.ProjectId)
                .setDatabaseUrl(firebaseConfig.DatabaseUrl)
                .build()

FirebaseApp.initializeApp(context, options, "SDK")

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

            val token = task.result
            Log.d("Test", token)
        })

As soon as I make any change or remove the json file the project crashes. I have seen and read the previous threads 1 2 3 which suggests to put the google-service.json file on the client side and manage it from there, but I want to know why can't I use my previous approach with FirebaseOptions Builder or is there any other possible way to manage the notification on SDK part. Also, would it be a good idea to revert back to old version and keep it operating the same way?


Solution

  • SOLUTION

    The following code is an alternative to setup firebase clould messaging without google-service.json file.

        val options = FirebaseOptions.Builder()
                    .setGcmSenderId(firebaseConfig!!.GcmSenderId)
                    .setApiKey(firebaseConfig.ApiKey)
                    .setApplicationId(firebaseConfig.ApplicationId)
                    .setStorageBucket(firebaseConfig.StorageBucket)
                    .setProjectId(firebaseConfig.ProjectId)
                    .setDatabaseUrl(firebaseConfig.DatabaseUrl)
                    .build()
    
        val app: FirebaseApp? = try {
            // Attempt to get the existing FirebaseApp instance
            FirebaseApp.getInstance("SDK")
        } catch (e: IllegalStateException) {
            // If it doesn't exist, initialize it
            FirebaseApp.initializeApp(context, options, "SDK")
        }
    
        val firebaseMessaging = app?.get(FirebaseMessaging::class.java) as FirebaseMessaging
        firebaseMessaging.token.addOnCompleteListener(OnCompleteListener { task ->
            if (!task.isSuccessful) {
                Log.w("LOG", "Fetching FCM registration token failed", task.exception)
                return@OnCompleteListener
            }
    
            // Get new FCM registration token
            val token = task.result
            Log.d("LOG", token)
        })