過去幾天我一直在使用 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
}
uj5u.com熱心網友回復:
令牌字串將以冒號結尾的假設是錯誤的......
這PushNotificationsFirebaseMessagingService只是回傳一個無效的令牌
并且問題沒有任何PushNotificationsFirebaseMessagingService.
if (token.isNullOrBlank() || !token.contains(":")) {
throw IllegalArgumentException("bad or no token given")
}
uj5u.com熱心網友回復:
發現問題了。一個微軟檔案有
FirebaseInstallations
.getInstance()
.id
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
return@OnCompleteListener
}
PushNotificationsFirebaseMessagingService.token = task.result
PushNotificationsFirebaseMessagingService.notificationRegistrationService?.refreshRegistration()
})
在創建。所以那個“令牌”只是安裝 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;
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/337405.html
標籤:安卓 火力基地 科特林 推送通知 firebase-cloud-消息
