Get FirebaseInstance Id from FirebaseMessaging.getInstance().token [duplicate]

I am updating dependencies in firebase-messaging in my project and found out that FirebaseInstanceId.getInstance().instance is no longer used.

In my MessagingService in onsuccesslistener i was getting two strings from:

                fun initFB() {
        synchronized(MyFirebaseMessagingService.TAG) {
            FirebaseInstallations.getInstance().id.addOnSuccessListener { result ->
                coroutineScope.launch(dispatcher) {
                    try {
                        
                        SP.firebaseInstanceId = result.id
                        SP.firebaseToken = result.token
                    } catch (e: Exception) {
                       // sth...
                    }
                }
            }
        }
    }

Now what I have is this new approach.

FirebaseMessaging.getInstance().token.addOnSuccessListener { result ->
            newtoken = result
}     

I was sending those id and token to SharedPreferences. How can I retrieve the id with this new aproach?


Solution 1:

According to the documentation.

This method is deprecated. Use FirebaseInstallations.getId() instead.

Edit:

private fun verifyTokenUpdate() {
    val fcmId = FirebaseInstallations.getInstance().id
    val fcmToken = FirebaseMessaging.getInstance().token
    fcmToken.addOnSuccessListener { tokenString ->
        val sharedPreferencesSession = getSharedPreferences("Session", Context.MODE_PRIVATE)
        if (sharedPreferencesSession.getString("FCMToken", "") != tokenString) {
            fcmId.addOnSuccessListener { newId ->
                registerNewToken(tokenString, newId)
            }
        }
    }
}