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



过去几天我一直在使用Azure通知中心,尝试在asp.net和Dart/Kotlin中设置推送通知。我一直在努力使用FCM/PNS代币。

当我注册我的应用程序时,我得到这个令牌:ddGYUP9OSdi2YR9Y******使用*以防万一。

在开发的某个时刻,我发现我有一个与Hubs相关的令牌注册:ddGYUP9OSdi2YR9Y******:APA91bMANCn_SZQV8bUJCWOiyPzdXaBPrqLmqIk8ELj6RfCx5TKNR2hLmiNMfuyK7LdY70-BtMxxyRbituhPH2t5v9p0A-8qkCleEgOWi4cXcvKpxedW2QmqEmym-hk8oZOXdx-*****

这是相同的标记,但在分号之后添加了一些内容。这是什么?它从何而来?

我从FirebaseInstallations.getInstance().id获得第一个令牌,并且我用令牌注册的每个设备都是相似的长度。然而,在我的asp.net项目中,向设备发送通知仅适用于较长的令牌。当我使用Firebase控制台测试通知时:Firebase - Engage - Cloud Messaging - Compose notification,只有长通知有效。这让我相信我的注册码有问题。

那么短标记上冒号之后的额外内容是什么?

为感兴趣的人获取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
}

假设令牌字符串将以冒号结束,这是错误的…
PushNotificationsFirebaseMessagingService只是返回一个无效的令牌
,问题没有任何PushNotificationsFirebaseMessagingService

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

找到问题。一个微软文档有

FirebaseInstallations
.getInstance()
.id
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
return@OnCompleteListener
}
PushNotificationsFirebaseMessagingService.token = task.result
PushNotificationsFirebaseMessagingService.notificationRegistrationService?.refreshRegistration()
})

In on create。因此,"token"只是安装Id。

令牌的正确代码,如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;
})

最新更新