From 1a9e271dbe438b7002afcb3b6c0923cc8cb76fe7 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Tue, 22 Mar 2022 20:30:41 -0400 Subject: [PATCH] Fix crash during UP registration, #185 --- .../io/heckel/ntfy/up/BroadcastReceiver.kt | 20 ++++++++++++------- .../main/java/io/heckel/ntfy/up/Constants.kt | 2 +- .../java/io/heckel/ntfy/up/Distributor.kt | 9 ++++++--- .../metadata/android/en-US/changelog/25.txt | 1 + 4 files changed, 21 insertions(+), 11 deletions(-) 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 339b772..43cfe09 100644 --- a/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt +++ b/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt @@ -5,8 +5,8 @@ import android.content.Intent import io.heckel.ntfy.R import io.heckel.ntfy.app.Application import io.heckel.ntfy.db.Subscription -import io.heckel.ntfy.util.Log import io.heckel.ntfy.service.SubscriberServiceManager +import io.heckel.ntfy.util.Log import io.heckel.ntfy.util.randomString import io.heckel.ntfy.util.topicUrlUp import kotlinx.coroutines.Dispatchers @@ -40,7 +40,7 @@ class BroadcastReceiver : android.content.BroadcastReceiver() { Log.d(TAG, "REGISTER received for app $appId (connectorToken=$connectorToken)") if (appId.isBlank()) { Log.w(TAG, "Refusing registration: Empty application") - distributor.sendRegistrationRefused(appId, connectorToken) + distributor.sendRegistrationFailed(appId, connectorToken, "Empty application string") return } GlobalScope.launch(Dispatchers.IO) { @@ -52,7 +52,7 @@ class BroadcastReceiver : android.content.BroadcastReceiver() { distributor.sendEndpoint(appId, connectorToken, endpoint) } else { Log.d(TAG, "Subscription with connectorToken $connectorToken exists for a different app. Refusing registration.") - distributor.sendRegistrationRefused(appId, connectorToken) + distributor.sendRegistrationFailed(appId, connectorToken, "Connector token already exists") } return@launch } @@ -74,11 +74,17 @@ class BroadcastReceiver : android.content.BroadcastReceiver() { lastActive = Date().time/1000 ) Log.d(TAG, "Adding subscription with for app $appId (connectorToken $connectorToken): $subscription") - repository.addSubscription(subscription) - distributor.sendEndpoint(appId, connectorToken, endpoint) + try { + // Note, this may fail due to a SQL constraint exception, see https://github.com/binwiederhier/ntfy/issues/185 + repository.addSubscription(subscription) + distributor.sendEndpoint(appId, connectorToken, endpoint) - // Refresh (and maybe start) foreground service - SubscriberServiceManager.refresh(app) + // Refresh (and maybe start) foreground service + SubscriberServiceManager.refresh(app) + } catch (e: Exception) { + Log.w(TAG, "Failed to add subscription", e) + distributor.sendRegistrationFailed(appId, connectorToken, e.message) + } } } 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 8c9ddaf..ab9c816 100644 --- a/app/src/main/java/io/heckel/ntfy/up/Constants.kt +++ b/app/src/main/java/io/heckel/ntfy/up/Constants.kt @@ -6,7 +6,7 @@ package io.heckel.ntfy.up */ const val ACTION_NEW_ENDPOINT = "org.unifiedpush.android.connector.NEW_ENDPOINT" -const val ACTION_REGISTRATION_REFUSED = "org.unifiedpush.android.connector.REGISTRATION_REFUSED" +const val ACTION_REGISTRATION_FAILED = "org.unifiedpush.android.connector.REGISTRATION_FAILED" const val ACTION_UNREGISTERED = "org.unifiedpush.android.connector.UNREGISTERED" const val ACTION_MESSAGE = "org.unifiedpush.android.connector.MESSAGE" diff --git a/app/src/main/java/io/heckel/ntfy/up/Distributor.kt b/app/src/main/java/io/heckel/ntfy/up/Distributor.kt index 1e9b7c1..5a11d6b 100644 --- a/app/src/main/java/io/heckel/ntfy/up/Distributor.kt +++ b/app/src/main/java/io/heckel/ntfy/up/Distributor.kt @@ -39,12 +39,15 @@ class Distributor(val context: Context) { context.sendBroadcast(broadcastIntent) } - fun sendRegistrationRefused(app: String, connectorToken: String) { - Log.d(TAG, "Sending REGISTRATION_REFUSED to $app (token=$connectorToken)") + fun sendRegistrationFailed(app: String, connectorToken: String, message: String?) { + Log.d(TAG, "Sending REGISTRATION_FAILED to $app (token=$connectorToken)") val broadcastIntent = Intent() broadcastIntent.`package` = app - broadcastIntent.action = ACTION_REGISTRATION_REFUSED + broadcastIntent.action = ACTION_REGISTRATION_FAILED broadcastIntent.putExtra(EXTRA_TOKEN, connectorToken) + if (message != null) { + broadcastIntent.putExtra(EXTRA_MESSAGE, message) + } context.sendBroadcast(broadcastIntent) } diff --git a/fastlane/metadata/android/en-US/changelog/25.txt b/fastlane/metadata/android/en-US/changelog/25.txt index 18a2d91..beb63f8 100644 --- a/fastlane/metadata/android/en-US/changelog/25.txt +++ b/fastlane/metadata/android/en-US/changelog/25.txt @@ -4,6 +4,7 @@ Features: Bugs: * IllegalStateException: Failed to build unique file (#177, thanks to @Fallenbagel for reporting) +* SQLiteConstraintException: Crash during UP registration (#185) Thanks: * Many thanks to @cmeis, @Fallenbagel, @J117 and @rogeliodh for input on the new attachment logic, and for