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/foss/java/org/openhab/habdroid/core/NotificationPoller.kt b/mobile/src/foss/java/org/openhab/habdroid/core/NotificationPoller.kt index 9bf832a6fa..3e8bb5c711 100644 --- a/mobile/src/foss/java/org/openhab/habdroid/core/NotificationPoller.kt +++ b/mobile/src/foss/java/org/openhab/habdroid/core/NotificationPoller.kt @@ -67,7 +67,6 @@ object NotificationPoller { newMessages.forEach { message -> notifHelper.showNotification( - message.id.hashCode(), message, null, null diff --git a/mobile/src/full/java/org/openhab/habdroid/core/CloudMessagingHelper.kt b/mobile/src/full/java/org/openhab/habdroid/core/CloudMessagingHelper.kt index 0295a9557a..43bc8e2ca5 100644 --- a/mobile/src/full/java/org/openhab/habdroid/core/CloudMessagingHelper.kt +++ b/mobile/src/full/java/org/openhab/habdroid/core/CloudMessagingHelper.kt @@ -40,12 +40,8 @@ object CloudMessagingHelper { } } - fun onNotificationSelected(context: Context, intent: Intent) { - val notificationId = intent.getIntExtra(NotificationHelper.EXTRA_NOTIFICATION_ID, -1) - if (notificationId >= 0) { - FcmRegistrationWorker.scheduleHideNotification(context, notificationId) - } - } + @Suppress("UNUSED_PARAMETER") + fun onNotificationSelected(context: Context, intent: Intent) {} fun isPollingBuild() = false diff --git a/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt b/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt index afdd1d355c..b2e2b544b5 100644 --- a/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt +++ b/mobile/src/full/java/org/openhab/habdroid/core/FcmMessageListenerService.kt @@ -39,7 +39,6 @@ class FcmMessageListenerService : FirebaseMessagingService() { val data = message.data Log.d(TAG, "onMessageReceived with data $data") val messageType = data["type"] ?: return - val notificationId = data["notificationId"]?.toInt() ?: 1 when (messageType) { "notification" -> { @@ -55,20 +54,15 @@ class FcmMessageListenerService : FirebaseMessagingService() { ) runBlocking { - val context = this@FcmMessageListenerService notifHelper.showNotification( - notificationId, cloudNotification, - FcmRegistrationWorker.createHideNotificationIntent(context, notificationId), - FcmRegistrationWorker.createHideNotificationIntent( - context, - NotificationHelper.SUMMARY_NOTIFICATION_ID - ) + null, + null ) } } "hideNotification" -> { - notifHelper.cancelNotification(notificationId) + notifHelper.cancelNotification(data["persistedId"].orEmpty().hashCode()) } } } 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..e61b6d0198 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,12 @@ 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 @@ -100,13 +99,6 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete return retryOrFail() } } - ACTION_HIDE_NOTIFICATION -> { - val id = inputData.getInt(KEY_NOTIFICATION_ID, -1) - if (id >= 0) { - sendHideNotificationRequest(id, connection.messagingSenderId) - return Result.success() - } - } else -> Log.e(TAG, "Invalid action '$action'") } @@ -121,34 +113,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") - } - - private fun sendHideNotificationRequest(notificationId: Int, senderId: String) { - val fcm = FirebaseMessaging.getInstance() - val message = RemoteMessage.Builder("$senderId@gcm.googleapis.com") - .addData("type", "hideNotification") - .addData("notificationId", notificationId.toString()) - .build() - fcm.send(message) + 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") + } } class ProxyReceiver : BroadcastReceiver() { @@ -181,7 +164,6 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete private val TAG = FcmRegistrationWorker::class.java.simpleName private const val ACTION_REGISTER = "org.openhab.habdroid.action.REGISTER_GCM" - private const val ACTION_HIDE_NOTIFICATION = "org.openhab.habdroid.action.HIDE_NOTIFICATION" private const val KEY_ACTION = "action" private const val KEY_NOTIFICATION_ID = "notificationId" @@ -193,23 +175,6 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete enqueueFcmWorker(context, data) } - internal fun scheduleHideNotification(context: Context, notificationId: Int) { - val data = Data.Builder() - .putString(KEY_ACTION, ACTION_HIDE_NOTIFICATION) - .putInt(KEY_NOTIFICATION_ID, notificationId) - .build() - - enqueueFcmWorker(context, data) - } - - internal fun createHideNotificationIntent(context: Context, notificationId: Int): PendingIntent { - val intent = Intent(context, FcmRegistrationWorker::class.java) - .setAction(ACTION_HIDE_NOTIFICATION) - .putExtra(KEY_NOTIFICATION_ID, notificationId) - - return ProxyReceiver.wrap(context, intent, notificationId) - } - private fun enqueueFcmWorker(context: Context, data: Data) { val constraints = Constraints.Builder() .setRequiredNetworkType(NetworkType.CONNECTED) diff --git a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt index eebb85e864..6f6f6ec2d9 100644 --- a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt +++ b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt @@ -46,16 +46,15 @@ class NotificationHelper(private val context: Context) { private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager suspend fun showNotification( - notificationId: Int, message: CloudNotification, deleteIntent: PendingIntent?, summaryDeleteIntent: PendingIntent? ) { createChannelForSeverity(message.severity) - val n = makeNotification(message, notificationId, deleteIntent) + val n = makeNotification(message, message.idHash, deleteIntent) - notificationManager.notify(notificationId, n) + notificationManager.notify(message.idHash, n) if (HAS_GROUPING_SUPPORT) { val count = countCloudNotifications(notificationManager.activeNotifications) @@ -218,7 +217,6 @@ class NotificationHelper(private val context: Context) { val contentIntent = Intent(context, MainActivity::class.java).apply { action = MainActivity.ACTION_NOTIFICATION_SELECTED flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP - putExtra(EXTRA_NOTIFICATION_ID, notificationId) putExtra(MainActivity.EXTRA_PERSISTED_NOTIFICATION_ID, persistedId) } return PendingIntent.getActivity( @@ -249,8 +247,6 @@ class NotificationHelper(private val context: Context) { "severity-$severity" } - @Suppress("MemberVisibilityCanBePrivate") // Used in full flavor - internal const val EXTRA_NOTIFICATION_ID = "notificationId" internal const val SUMMARY_NOTIFICATION_ID = 0 // Notification grouping is only available on N or higher, as mentioned in diff --git a/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt b/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt index c71de81caa..6dc7d64e8f 100644 --- a/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt +++ b/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt @@ -30,7 +30,9 @@ data class CloudNotification internal constructor( val createdTimestamp: Long, val icon: IconResource?, val severity: String? -) : Parcelable +) : Parcelable { + val idHash get() = id.hashCode() +} @Throws(JSONException::class) fun JSONObject.toCloudNotification(): CloudNotification {