Firebase (Fcm) How to Get Token

Get fcm token when user is logged for the first time

There is a mistake in your code.

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

task.isSuccessful block should store the token but it was return and loging error ?.

Ref : https://firebase.google.com/docs/cloud-messaging/android/client#retrieve-the-current-registration-token

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 = getString(R.string.msg_token_fmt, token)
Log.d(TAG, msg)
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
})

How to get Android Device Token in Kotlin for Firebase Cloud Messaging

FirebaseInstallations isn't the correct token for FCM! I had to use FirebaseMessaging and use the following:

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

val token = task.result

Log.d(TAG, token)

})

How to get the device token in flutter

You have to create the FirebaseMessaging instance. Change to:

@override
void initState() {
FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance; // Change here
_firebaseMessaging.getToken().then((token){
print("token is $token");
});
}

Verify if fcm token belongs to an android or ios device

The following might help you to customize notification messages based on the platform:

https://firebase.google.com/docs/cloud-messaging/send-message#customize-messages-across-platforms

If you want to distinguish the tokens manually, then a possible hack would be to add the platform name as a prefix when storing tokens in a database from your frontend app. Then, you can filter out the tokens on your backend by implementing a programming logic i.e. creating separate arrays for android and IOS tokens.

I don't think there is a way to just see the token and tell from which platform it belongs.



Related Topics



Leave a reply



Submit