Fix crash during UP registration, #185

This commit is contained in:
Philipp Heckel 2022-03-22 20:30:41 -04:00
parent c55693f9cf
commit 1a9e271dbe
4 changed files with 21 additions and 11 deletions

View file

@ -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)
}
}
}

View file

@ -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"

View file

@ -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)
}

View file

@ -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