I am implementing Firebase on android using kotlin.
as the FirebaseInstanceId.getInstance().token is depriciated, I am using the following function inside the companion object of my MyFirebaseMessageService class to get the token:
companion object
{
fun grabFcmToken():String?
{
var s:String?=null
FirebaseInstanceId.getInstance().
instanceId.addOnCompleteListener {task ->
if (task.isSuccessful)
{
s=task.result!!.token
Log.i("token used is ", s)
}
else
{
throw Exception("Can't get firebase token")
}
}
Log.i("To be returned token ", s)
return s
}
}
Android Studio highlights s as Wrapped into a reference object to be modified when captured in a closure
Upon the execution of grabFcmToken() the first log prints the token successfully, but the second log has a java.lang.NullPointerException: println needs a message indicating that s has the value null and hence it's value is unchanged.
how may i modify s such that the captured token is being reflected in it?