Home > database >  Android Firebase Cloud Messaging Token (FCM) token is too short/incomplete
Android Firebase Cloud Messaging Token (FCM) token is too short/incomplete

Time:10-27

I've been working with Azure Notification Hub the past few days, trying to set up push notifications in ASP NET and Dart/Kotlin. I've been struggling with FCM/PNS tokens.

When I register my app, I get this token: ddGYUP9OSdi2YR9Y****** using * just in case.

At one point in development, I found I had a registration associated with Hubs with the token: ddGYUP9OSdi2YR9Y******:APA91bMANCn_SZQV8bUJCWOiyPzdXaBPrqLmqIk8ELj6RfCx5TKNR2hLmiNMfuyK7LdY70-BtMxxyRbituhPH2t5v9p0A-8qkCleEgOWi4cXcvKpxedW2QmqEmym-hk8oZOXdx-*****

It's the same token, but with something added after the semi colon. What is this, and where does it come from?

I get the first token from FirebaseInstallations.getInstance().id, and with every device I register with the tokens are a similar length. However in my ASP NET project, sending a notification to a device only works with the longer token. When I test a notification using the Firebase Console: Firebase - Engage - Cloud Messaging - Compose Notification, only the long one works. Which leads me to believe there's a problem in my registration code.

So what is that extra stuff after the colon on the short token?

My code for getting the FCM token for those interested.

private fun getDeviceToken() : String {
  if(!playServicesAvailable) {
    throw Exception(getPlayServicesError())
  }

  val token = PushNotificationsFirebaseMessagingService.token

  if (token.isNullOrBlank()) {
    throw Exception("Unable to resolve token for FCM.")
  }

  return token
}

CodePudding user response:

The assumption, that the token string would end at the colon, is just wrong ...
That PushNotificationsFirebaseMessagingService simply returns an invalid token
and the question doesn't have any PushNotificationsFirebaseMessagingService.

if (token.isNullOrBlank() || !token.contains(":")) {
    throw IllegalArgumentException("bad or no token given")
}

CodePudding user response:

Found the problem. A microsoft doc had

FirebaseInstallations
                    .getInstance()
                    .id
                    .addOnCompleteListener(OnCompleteListener { task ->
                        if (!task.isSuccessful) {
                            return@OnCompleteListener
                        }

                        PushNotificationsFirebaseMessagingService.token = task.result
                        PushNotificationsFirebaseMessagingService.notificationRegistrationService?.refreshRegistration()
                    })

In on create. So that 'token' was just the installation Id.

The correct code for the token, as shown in Firebase docs is

FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
                if(!task.isSuccessful) {
                    print("Fetching FCM registration token failed")
                    return@OnCompleteListener
                }

                val token = task.result
                PushNotificationsFirebaseMessagingService.token = token;
            })
  • Related