简体   繁体   English

Android Firebase 云消息传递令牌 (FCM) 令牌太短/不完整

[英]Android Firebase Cloud Messaging Token (FCM) token is too short/incomplete

I've been working with Azure Notification Hub the past few days, trying to set up push notifications in ASP NET and Dart/Kotlin.过去几天我一直在使用 Azure 通知中心,尝试在 ASP NET 和 Dart/Kotlin 中设置推送通知。 I've been struggling with FCM/PNS tokens.我一直在为 FCM/PNS 令牌苦苦挣扎。

When I register my app, I get this token: ddGYUP9OSdi2YR9Y****** using * just in case.当我注册我的应用程序时,我得到这个令牌: ddGYUP9OSdi2YR9Y******使用 * 以防万一。

At one point in development, I found I had a registration associated with Hubs with the token: ddGYUP9OSdi2YR9Y******:APA91bMANCn_SZQV8bUJCWOiyPzdXaBPrqLmqIk8ELj6RfCx5TKNR2hLmiNMfuyK7LdY70-BtMxxyRbituhPH2t5v9p0A-8qkCleEgOWi4cXcvKpxedW2QmqEmym-hk8oZOXdx-*****在开发一个点上,我发现我与集线器与令牌相关联的注册: 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.我从FirebaseInstallations.getInstance().id获得第一个令牌,并且我使用令牌注册的每个设备的长度都相似。 However in my ASP NET project, sending a notification to a device only works with the longer token.但是,在我的 ASP NET 项目中,向设备发送通知仅适用于较长的令牌。 When I test a notification using the Firebase Console: Firebase - Engage - Cloud Messaging - Compose Notification, only the long one works.当我使用 Firebase 控制台测试通知时:Firebase - Engage - Cloud Messaging - Compose Notification,只有长的有效。 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.我为感兴趣的人获取 FCM 令牌的代码。

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
}

The assumption, that the token string would end at the colon, is just wrong ...令牌字符串将以冒号结尾的假设是错误的......
That PushNotificationsFirebaseMessagingService simply returns an invalid token PushNotificationsFirebaseMessagingService只是返回一个无效的令牌
and the question doesn't have any PushNotificationsFirebaseMessagingService .并且问题没有任何PushNotificationsFirebaseMessagingService

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

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.所以那个“令牌”只是安装 ID。

The correct code for the token, as shown in Firebase docs is令牌的正确代码,如 Firebase 文档中所示

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 云消息传递:FCM 令牌到期 - Firebase Cloud Messaging : Expiration of FCM token 在FCM(Firebase云消息传递)中,在通知中包含令牌是否安全? - In FCM (Firebase cloud messaging), is it safe to include token in the notification? Firebase Cloud Messaging(FCM)注册令牌服务器端验证 - Firebase Cloud Messaging (FCM) registration token server-side validation 如何向FCM(Firebase云消息传递)令牌的特定用户发送消息? - How to send a message to a specific user of an FCM (Firebase Cloud Messaging) token? 是一个设备还是一个帐户的FCM(firebase云消息传递)令牌? - Is FCM (firebase cloud messaging) Token for one device or for one account? Android,FireBase 云消息传递,(FCM) - Android, FireBase Cloud Messaging, (FCM) Firebase 云消息传递令牌未生成 - Firebase Cloud Messaging token is not generating 是否可以在 Android 中自定义 Firebase Cloud Messaging 生成的令牌? - Is it possible to customize the token generated by Firebase Cloud Messaging in Android? 如何在 Firebase 云消息传递的 Kotlin 中获取 Android 设备令牌 - How to get Android Device Token in Kotlin for Firebase Cloud Messaging Firebase FCM令牌作为Android的安全令牌 - Firebase FCM token as security token for Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM