From 66d9c1359e55bbde325b3a3640ec0bbe2efd8a82 Mon Sep 17 00:00:00 2001 From: mueller-ma Date: Fri, 23 Dec 2022 14:16:14 +0100 Subject: [PATCH] Update firebase to latest version Signed-off-by: mueller-ma --- mobile/build.gradle | 2 +- .../habdroid/core/FcmRegistrationWorker.kt | 40 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/mobile/build.gradle b/mobile/build.gradle index 458023f1b1..b0ac8a224b 100644 --- a/mobile/build.gradle +++ b/mobile/build.gradle @@ -182,7 +182,7 @@ dependencies { implementation "com.mikepenz:aboutlibraries:$about_libraries_version" // Firebase - implementation platform("com.google.firebase:firebase-bom:25.13.0") + implementation platform("com.google.firebase:firebase-bom:33.0.0") fullImplementation "com.google.firebase:firebase-messaging-ktx" fullImplementation "com.google.firebase:firebase-crashlytics-ktx" diff --git a/mobile/src/full/java/org/openhab/habdroid/core/FcmRegistrationWorker.kt b/mobile/src/full/java/org/openhab/habdroid/core/FcmRegistrationWorker.kt index c9ce94afd5..25227adf15 100644 --- a/mobile/src/full/java/org/openhab/habdroid/core/FcmRegistrationWorker.kt +++ b/mobile/src/full/java/org/openhab/habdroid/core/FcmRegistrationWorker.kt @@ -30,13 +30,13 @@ import androidx.work.OneTimeWorkRequest import androidx.work.WorkManager import androidx.work.WorkRequest import androidx.work.WorkerParameters -import com.google.firebase.iid.FirebaseInstanceId import com.google.firebase.messaging.FirebaseMessaging import com.google.firebase.messaging.RemoteMessage import java.io.IOException import java.net.URLEncoder import java.util.Locale import java.util.concurrent.TimeUnit +import kotlinx.coroutines.runBlocking import org.openhab.habdroid.R import org.openhab.habdroid.core.connection.CloudConnection import org.openhab.habdroid.core.connection.ConnectionFactory @@ -121,25 +121,25 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete // HttpException is thrown by our HTTP code, IOException can be thrown by FCM @Throws(HttpClient.HttpException::class, IOException::class) private suspend fun registerFcm(connection: CloudConnection) { - val token = FirebaseInstanceId.getInstance().getToken( - connection.messagingSenderId, - FirebaseMessaging.INSTANCE_ID_SCOPE - ) - val deviceName = deviceName + if (Util.isFlavorBeta) " (${context.getString(R.string.beta)})" else "" - val deviceId = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID) + - if (Util.isFlavorBeta) "-beta" else "" - - val regUrl = String.format( - Locale.US, - "addAndroidRegistration?deviceId=%s&deviceModel=%s®Id=%s", - deviceId, - URLEncoder.encode(deviceName, "UTF-8"), - token - ) - - Log.d(TAG, "Register device at openHAB-cloud with URL: $regUrl") - connection.httpClient.get(regUrl).close() - Log.d(TAG, "FCM reg id success") + FirebaseMessaging.getInstance().token.addOnSuccessListener { token: String -> + val deviceName = deviceName + if (Util.isFlavorBeta) " (${context.getString(R.string.beta)})" else "" + val deviceId = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID) + + if (Util.isFlavorBeta) "-beta" else "" + + val registrationUrl = String.format( + Locale.US, + "addAndroidRegistration?deviceId=%s&deviceModel=%s®Id=%s", + deviceId, + URLEncoder.encode(deviceName, "UTF-8"), + token + ) + + Log.d(TAG, "Register device at openHAB cloud with URL: $registrationUrl") + runBlocking { + connection.httpClient.get(registrationUrl).close() + } + Log.d(TAG, "FCM reg id success") + } } private fun sendHideNotificationRequest(notificationId: Int, senderId: String) {