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 6c097808..7c68113b 100644 --- a/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt +++ b/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt @@ -44,12 +44,13 @@ class BroadcastReceiver : android.content.BroadcastReceiver() { Log.d(TAG, "REGISTER received for app $appId (connectorToken=$connectorToken)") if (!repository.getUnifiedPushEnabled()) { Log.w(TAG, "Refusing registration because 'EnableUP' is disabled") - distributor.sendRegistrationFailed(appId, connectorToken, "UnifiedPush is disabled in ntfy") + // Action required: tell the app to not try again before an action as be done manually + // by the user + distributor.sendRegistrationFailed(appId, connectorToken, FailedReason.ACTION_REQUIRED) return } if (appId.isBlank()) { Log.w(TAG, "Refusing registration: Empty application") - distributor.sendRegistrationFailed(appId, connectorToken, "Empty application string") return } GlobalScope.launch(Dispatchers.IO) { @@ -65,7 +66,8 @@ 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.sendRegistrationFailed(appId, connectorToken, "Connector token already exists") + // Internal_error: try again with a new token + distributor.sendRegistrationFailed(appId, connectorToken, FailedReason.INTERNAL_ERROR) } return@launch } @@ -103,7 +105,8 @@ class BroadcastReceiver : android.content.BroadcastReceiver() { SubscriberServiceManager.refresh(app) } catch (e: Exception) { Log.w(TAG, "Failed to add subscription", e) - distributor.sendRegistrationFailed(appId, connectorToken, e.message) + // Try again when there is network + distributor.sendRegistrationFailed(appId, connectorToken, FailedReason.NETWORK) } // Add to log scrubber 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 7db2680e..60da3a2c 100644 --- a/app/src/main/java/io/heckel/ntfy/up/Constants.kt +++ b/app/src/main/java/io/heckel/ntfy/up/Constants.kt @@ -17,5 +17,5 @@ const val EXTRA_APPLICATION = "application" const val EXTRA_PI = "pi" const val EXTRA_TOKEN = "token" const val EXTRA_ENDPOINT = "endpoint" -const val EXTRA_MESSAGE = "message" +const val EXTRA_FAILED_REASON = "reason" const val EXTRA_BYTES_MESSAGE = "bytesMessage" 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 b6f9cce8..81f6ec36 100644 --- a/app/src/main/java/io/heckel/ntfy/up/Distributor.kt +++ b/app/src/main/java/io/heckel/ntfy/up/Distributor.kt @@ -15,7 +15,6 @@ class Distributor(val context: Context) { broadcastIntent.`package` = app broadcastIntent.action = ACTION_MESSAGE broadcastIntent.putExtra(EXTRA_TOKEN, connectorToken) - broadcastIntent.putExtra(EXTRA_MESSAGE, String(message)) // UTF-8 broadcastIntent.putExtra(EXTRA_BYTES_MESSAGE, message) context.sendBroadcast(broadcastIntent) } @@ -39,15 +38,13 @@ class Distributor(val context: Context) { context.sendBroadcast(broadcastIntent) } - fun sendRegistrationFailed(app: String, connectorToken: String, message: String?) { + fun sendRegistrationFailed(app: String, connectorToken: String, reason: FailedReason) { Log.d(TAG, "Sending REGISTRATION_FAILED to $app (token=$connectorToken)") val broadcastIntent = Intent() broadcastIntent.`package` = app broadcastIntent.action = ACTION_REGISTRATION_FAILED broadcastIntent.putExtra(EXTRA_TOKEN, connectorToken) - if (message != null) { - broadcastIntent.putExtra(EXTRA_MESSAGE, message) - } + broadcastIntent.putExtra(EXTRA_FAILED_REASON, reason) context.sendBroadcast(broadcastIntent) } diff --git a/app/src/main/java/io/heckel/ntfy/up/FailedReason.kt b/app/src/main/java/io/heckel/ntfy/up/FailedReason.kt new file mode 100644 index 00000000..59bb8997 --- /dev/null +++ b/app/src/main/java/io/heckel/ntfy/up/FailedReason.kt @@ -0,0 +1,23 @@ +package io.heckel.ntfy.up + +/** + * A registration request may fail for different reasons. + */ +enum class FailedReason { + /** + * This is a generic error type, you can try to register again directly. + */ + INTERNAL_ERROR, + /** + * The registration failed because of missing network connection, try again when network is back. + */ + NETWORK, + /** + * The distributor requires a user action to work. For instance, the distributor may be log out of the push server and requires the user to log in. The user must interact with the distributor or sending a new registration will fail again. + */ + ACTION_REQUIRED, + /* + * The distributor requires a VAPID key and the app didn't provide one during registration. + VAPID_REQUIRED, + */ +} \ No newline at end of file