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 0295a9557a6..43bc8e2ca58 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 afdd1d355c8..b2e2b544b5c 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 25227adf15d..e61b6d0198c 100644 --- a/mobile/src/full/java/org/openhab/habdroid/core/FcmRegistrationWorker.kt +++ b/mobile/src/full/java/org/openhab/habdroid/core/FcmRegistrationWorker.kt @@ -31,7 +31,6 @@ import androidx.work.WorkManager import androidx.work.WorkRequest import androidx.work.WorkerParameters import com.google.firebase.messaging.FirebaseMessaging -import com.google.firebase.messaging.RemoteMessage import java.io.IOException import java.net.URLEncoder import java.util.Locale @@ -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'") } @@ -142,15 +134,6 @@ class FcmRegistrationWorker(private val context: Context, params: WorkerParamete } } - 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) - } - class ProxyReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val actual = intent.parcelable("intent") ?: return @@ -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 eebb85e8642..6f6f6ec2d90 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 c71de81caa5..6dc7d64e8ff 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 {