From a6cd89247c2762edf4d5c4d2ff6b197af334446a Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Thu, 4 Jul 2024 04:43:00 +0000 Subject: [PATCH] Fix registration on unified poc app --- .../java/io/heckel/ntfy/ui/MainActivity.kt | 3 ++- .../io/heckel/ntfy/up/BroadcastReceiver.kt | 20 ++++++++++++++----- .../main/java/io/heckel/ntfy/up/Constants.kt | 3 +++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt index 61b1cae..b44acab 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt @@ -40,6 +40,7 @@ import io.heckel.ntfy.msg.DownloadType import io.heckel.ntfy.msg.NotificationDispatcher import io.heckel.ntfy.service.SubscriberService import io.heckel.ntfy.service.SubscriberServiceManager +import io.heckel.ntfy.up.TOPIC_MURENA import io.heckel.ntfy.util.* import io.heckel.ntfy.work.DeleteWorker import io.heckel.ntfy.work.PollWorker @@ -203,7 +204,7 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc lifecycleScope.launch(Dispatchers.IO) { val subscriptions = repository.getSubscriptions() - val defaultTopic = "murena_notification" + val defaultTopic = TOPIC_MURENA val hasTestTopic = subscriptions.any { it.topic == defaultTopic } if (hasTestTopic) { return@launch diff --git a/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt b/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt index e23857f..e30f0ed 100644 --- a/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt +++ b/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt @@ -69,12 +69,17 @@ class BroadcastReceiver : android.content.BroadcastReceiver() { // Add subscription val baseUrl = repository.getDefaultBaseUrl() ?: context.getString(R.string.app_base_url) var topic = UP_PREFIX + randomString(TOPIC_RANDOM_ID_LENGTH) - if (appId == "foundation.e.unifiedpoc") { - topic = "murena_notification" + if (appId == PACKAGE_MURENA_UNIFIED_PUSH) { + topic = TOPIC_MURENA } val endpoint = topicUrlUp(baseUrl, topic) + val subscriptionId = + appId.takeIf { it == PACKAGE_MURENA_UNIFIED_PUSH }?.run { + repository.getSubscriptions() + .find { it.baseUrl == baseUrl && it.topic == topic }?.id + } ?: randomSubscriptionId() val subscription = Subscription( - id = randomSubscriptionId(), + id = subscriptionId, baseUrl = baseUrl, topic = topic, instant = true, // No Firebase, always instant! @@ -92,10 +97,15 @@ class BroadcastReceiver : android.content.BroadcastReceiver() { newCount = 0, lastActive = Date().time/1000 ) - Log.d(TAG, "Adding subscription with for app $appId (connectorToken $connectorToken): $subscription") try { // Note, this may fail due to a SQL constraint exception, see https://github.com/binwiederhier/ntfy/issues/185 - repository.addSubscription(subscription) + if (appId == PACKAGE_MURENA_UNIFIED_PUSH) { + Log.d(TAG, "Updating subscription with for app $appId (connectorToken $connectorToken): $subscription") + repository.updateSubscription(subscription) + } else { + Log.d(TAG, "Adding subscription with for app $appId (connectorToken $connectorToken): $subscription") + repository.addSubscription(subscription) + } distributor.sendEndpoint(appId, connectorToken, endpoint) // Refresh (and maybe start) foreground service diff --git a/app/src/main/java/io/heckel/ntfy/up/Constants.kt b/app/src/main/java/io/heckel/ntfy/up/Constants.kt index ab9c816..a94fc56 100644 --- a/app/src/main/java/io/heckel/ntfy/up/Constants.kt +++ b/app/src/main/java/io/heckel/ntfy/up/Constants.kt @@ -20,3 +20,6 @@ const val EXTRA_TOKEN = "token" const val EXTRA_ENDPOINT = "endpoint" const val EXTRA_MESSAGE = "message" const val EXTRA_BYTES_MESSAGE = "bytesMessage" + +const val PACKAGE_MURENA_UNIFIED_PUSH = "foundation.e.unifiedpoc" +const val TOPIC_MURENA = "murena_notification"